Keyhighlighter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 92
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A replace() 0 23 5
A highlight() 0 8 1
1
<?php
2
3
namespace XoopsModules\Lexikon;
4
5
6
/**
7
 * This file contains the keyhighlighter class that highlight the chosen keyword in the current output buffer.
8
 *
9
 * @package keyhighlighter
10
 */
11
12
/**
13
 * keyhighlighter class
14
 *
15
 * This class highlight the chosen keywords in the current output buffer
16
 *
17
 * @package   keyhighlighter
18
 * @author    Setec Astronomy
19
 * @abstract  Highlight specific keywords.
20
 * @copyright 2004
21
 * @example   sample.php A sample code.
22
 * @link      http://setecastronomy.stufftoread.com
23
 */
24
class Keyhighlighter
25
{
26
    /**
27
     * @access private
28
     */
29
    public $preg_keywords = '';
30
    /**
31
     * @access private
32
     */
33
    public $keywords = '';
34
    /**
35
     * @access private
36
     */
37
    public $singlewords = false;
38
    /**
39
     * @access private
40
     */
41
    public $replace_callback = null;
42
    public $content;
43
    /**
44
     * Main constructor
45
     *
46
     * This is the main constructor of keyhighlighter class. <br>
47
     * It's the only public method of the class.
48
     * @param string   $keywords         the keywords you want to highlight
49
     * @param bool     $singlewords      specify if it has to highlight also the single words.
50
     * @param callback $replace_callback a custom callback for keyword highlight.
51
     *                                   <code>
52
     *                                   <?php
53
     *                                   require_once ('keyhighlighter.class.php');
54
     *
55
     * function my_highlighter ($matches) {
56
     *    return '<span style="font-weight: bolder; color: #FF0000;">' . $matches[0] . '</span>';
57
     * }
58
     *
59
     * new keyhighlighter ('W3C', false, 'my_highlighter');
60
     * readfile ('http://www.w3c.org/');
61
     * ?>
62
     * </code>
63
     */
64
    // public function __construct ()
65
    public function __construct($keywords, $singlewords = false, $replace_callback = null)
66
    {
67
        $this->keywords         = $keywords;
68
        $this->singlewords      = $singlewords;
69
        $this->replace_callback = $replace_callback;
70
        //ob_start ([$this, 'highlight']);
71
    }
72
73
    /**
74
     * @access private
75
     * @param $replace_matches
76
     * @return mixed
77
     */
78
    public function replace($replace_matches)
79
    {
80
        $patterns = [];
81
        if ($this->singlewords) {
82
            $keywords = \explode(' ', $this->preg_keywords);
83
            foreach ($keywords as $keyword) {
84
                $patterns[] = '/(?' . '>' . $keyword . '+)/si';
85
            }
86
        } else {
87
            $patterns[] = '/(?' . '>' . $this->preg_keywords . '+)/si';
88
        }
89
90
        $result = $replace_matches[0];
91
92
        foreach ($patterns as $pattern) {
93
            if (null !== $this->replace_callback) {
94
                $result = \preg_replace_callback($pattern, $this->replace_callback, $result);
95
            } else {
96
                $result = \preg_replace($pattern, '<span class="highlightedkey">\\0</span>', $result);
97
            }
98
        }
99
100
        return $result;
101
    }
102
103
    /**
104
     * @access private
105
     * @param $buffer
106
     * @return string
107
     */
108
    public function highlight($buffer)
109
    {
110
        $buffer              = '>' . $buffer . '<';
111
        $this->preg_keywords = \preg_replace('/[^\w ]/si', '', $this->keywords);
112
        $buffer              = \preg_replace_callback('/(\>(((?' . '>[^><]+)|(?R))*)\<)/is', [&$this, 'replace'], $buffer);
113
        $buffer              = \xoops_substr($buffer, 1, -1, $trimmarker = '');
114
115
        return $buffer;
116
    }
117
}
118