newbb_highlightText()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 2
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * NewBB 4.3x, the forum module for XOOPS project
4
 *
5
 * @copyright      XOOPS Project (https://xoops.org)
6
 * @license        http://www.fsf.org/copyleft/gpl.html GNU public license
7
 * @author         Taiwen Jiang (phppp or D.J.) <[email protected]>, irmtfan <[email protected]>
8
 * @since          4.3
9
 * @package        module::newbb
10
 */
11
12
13
14
define('NEWBB_FUNCTIONS_TEXT_LOADED', true);
15
16
if (!defined('NEWBB_FUNCTIONS_TEXT')) {
17
    define('NEWBB_FUNCTIONS_TEXT', 1);
18
    /**
19
     * function for select from a text where it have some keywords
20
     *
21
     * @param text    $text , array text $queryarray, int $selectlength = 200, int $selectstartlag = 100, int $highlight = true
0 ignored issues
show
Bug introduced by
The type text was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
     * @param         $queryarray
23
     * @param int     $selectstartlag
24
     * @param int     $selectlength
25
     * @param bool    $striptags
26
     * @param string  $excludetags
27
     * @param string  $start_trimmarker
28
     * @param string  $end_trimmarker
29
     * @return text   $select_text
30
     */
31
32
    function newbb_selectText(
33
        $text,
34
        $queryarray,
35
        $selectstartlag = 100,
36
        $selectlength = 200,
37
        $striptags = true,
38
        $excludetags = '<br>',
39
        $start_trimmarker = '[...]',
40
        $end_trimmarker = '[...]'
41
    ) {
42
        $sanitized_text       = $striptags ? strip_tags($text, $excludetags) : $text;
43
        $queryarray           = newbb_str2array($queryarray);
44
        $text_i               = strtolower($sanitized_text);
45
        $queryarray           = array_map('\strtolower', $queryarray);
46
        $lengtharray          = array_map('\strlen', $queryarray);
47
        $maxlengthquery       = max($lengtharray);
48
        $lengthend_trimmarker = strlen($end_trimmarker);
49
        $select_text          = '';
50
        $startpos             = 0;
51
        $endpos               = strlen($sanitized_text);
52
        while ($startpos < $endpos) {
53
            $pos = $endpos;
54
            foreach ($queryarray as $query) {
55
                if (false !== ($thispos = strpos($text_i, $query, $startpos))) {
56
                    $pos = min($thispos, $pos);
57
                }
58
            }
59
            if ($pos == $endpos) {
60
                break;
61
            }
62
            $start       = max($pos - $selectstartlag, $startpos - $maxlengthquery, 0); // $startpos is the last position in the previous select text
63
            $length      = $maxlengthquery + $selectlength; //xoops_local("strlen", $query) + 200;
64
            $select_text .= '<p>';
65
            $select_text .= ($start > 0) ? $start_trimmarker . ' ' : ' ';
66
            $select_text .= xoops_substr($sanitized_text, $start, $length + $lengthend_trimmarker + 1, ' ' . $end_trimmarker) . '</p>';
67
            $startpos    = $start + $length + 1; // start searching from next position.
68
        }
69
        if (empty($select_text)) {
70
            return '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return '' returns the type string which is incompatible with the documented return type text.
Loading history...
71
        } // if no text return empty string
72
        return '<span class="newbb_select_text">' . $select_text . '</span>';
0 ignored issues
show
Bug Best Practice introduced by
The expression return '<span class="new...select_text . '</span>' returns the type string which is incompatible with the documented return type text.
Loading history...
73
    }
74
75
    /**
76
     * function for highlight a text when it have some keywords
77
     *
78
     * @param text  $text , array text $queryarray
79
     * @param       $queryarray
80
     * @return text $highlight_text
81
     */
82
83
    function newbb_highlightText($text, $queryarray)
84
    {
85
        if (empty($GLOBALS['xoopsModuleConfig']['highlight_search_enable'])) {
86
            return $text;
87
        }
88
        $queryarray = newbb_str2array($queryarray);
89
        // if $queryarray is string
90
        $highlight_text = $text;
91
        foreach ($queryarray as $key => $query) {
92
            // use preg_replace instead of str_replace to exclude all $queries inside html span tag
93
            $highlight_text = preg_replace("/(?!(?:[^<]+>|[^>]+<\/a>))(" . preg_quote($query, '/') . ')/si', newbb_highlighter($query, $key), $highlight_text);
94
        }
95
96
        return $highlight_text;
97
    }
98
99
    /**
100
     * function for highlighting search results
101
     *
102
     * @param text     $query , int $i
103
     * @param          $i
104
     * @return mixed
105
     */
106
    function newbb_highlighter($query, $i)
107
    {
108
        return '<span class="newbb_highlight term' . $i . '">' . $query . '</span>';
109
    }
110
111
    /**
112
     * function for convert string to array
113
     *
114
     * @param text /array $str
0 ignored issues
show
Documentation Bug introduced by
The doc comment /array at position 0 could not be parsed: Unknown type name '/array' at position 0 in /array.
Loading history...
115
     * @return mixed
116
     */
117
118
    function newbb_str2array($str)
119
    {
120
        if (!is_array($str)) {
121
            // split the phrase by any number of commas or space characters,
122
            // which include " ", \r, \t, \n and \f
123
            $temp_str = preg_split('/[\s,]+/', $str);
124
            $strarray = [];
125
            foreach ($temp_str as $s) {
126
                $strarray[] = addslashes($s);
127
            }
128
129
            return $strarray;
130
        } else {
131
            return $str;
132
        }
133
    }
134
}
135