文字中的Higlight关键字-优化

时间:2018-12-06 00:30:33

标签: php iconv

我正在研究一个小的功能,该功能突出显示文本中的关键字。在我使用正则表达式将原来的文本替换为突出显示的文本(也可以使用)之前,但是最近我有时间重写此函数。我需要有关性能老虎钳或如何进行改进的一些帮助。任何有任何想法如何改进的人,我将不胜感激。

         home_team away_team  home_goals  away_goals label  home_team_goal_last_1  home_team_goal_last_3  home_team_goal_last_5  away_team_goal_last_1  away_team_goal_last_3  away_team_goal_last_5  DAY(match_date)  MONTH(match_date)  YEAR(match_date)  WEEKDAY(match_date)  PERCENTILE(home_goals)  PERCENTILE(away_goals)  PERCENTILE(home_team_goal_last_1)  PERCENTILE(home_team_goal_last_3)  PERCENTILE(home_team_goal_last_5)  PERCENTILE(away_team_goal_last_1)  PERCENTILE(away_team_goal_last_3)  PERCENTILE(away_team_goal_last_5)
match_id                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
1          Arsenal   Chelsea           2           0     1                    NaN                    NaN                    NaN                    NaN                    NaN                    NaN                1                  1              2014                    2                0.666667                0.166667                                NaN                                NaN                                NaN                                NaN                                NaN                                NaN
2          Arsenal   Chelsea           1           0     1                    2.0                    NaN                    NaN                    0.0                    NaN                    NaN                2                  1              2014                    3                0.333333                0.166667                           0.590909                                NaN                                NaN                           0.227273                                NaN                                NaN
3          Arsenal   Chelsea           0           3     2                    1.0                    NaN                    NaN                    0.0                    NaN                    NaN                3                  1              2014                    4                0.125000                0.958333                           0.272727                                NaN                                NaN                           0.227273                                NaN                                NaN
4          Chelsea   Arsenal           1           1     X                    3.0               1.000000                    NaN                    0.0               1.000000                    NaN                4                  1              2014                    5                0.333333                0.500000                           0.909091                           0.333333                                NaN                           0.227273                           0.500000                                NaN
5          Chelsea   Arsenal           2           0     1                    1.0               1.333333                    NaN                    1.0               0.666667                    NaN                5                  1              2014                    6                0.666667                0.166667                           0.272727                           0.555556                                NaN                           0.590909                           0.277778                                NaN
6          Chelsea   Arsenal           2           1     1                    2.0               2.000000                    1.2                    0.0               0.333333                    0.8                6                  1              2014                    0                0.666667                0.500000                           0.590909                           0.722222                           0.571429                           0.227273                           0.111111                           0.214286
7          Arsenal   Chelsea           2           2     X                    1.0               0.666667                    0.6                    2.0               1.666667                    1.6                7                  1              2014                    1                0.666667                0.791667                           0.272727                           0.111111                           0.142857                           0.909091                           0.833333                           0.785714
8          Arsenal   Chelsea           0           1     2                    2.0               1.000000                    0.8                    2.0               2.000000                    2.0                8                  1              2014                    2                0.125000                0.500000                           0.590909                           0.333333                           0.357143                           0.909091                           1.000000                           1.000000
9          Arsenal   Chelsea           1           3     2                    0.0               1.000000                    0.8                    1.0               1.666667                    1.6                9                  1              2014                    3                0.333333                0.958333                           0.090909                           0.333333                           0.357143                           0.590909                           0.833333                           0.785714
10         Chelsea   Arsenal           3           1     1                    3.0               2.000000                    2.0                    1.0               1.000000                    0.8               10                  1              2014                    4                0.916667                0.500000                           0.909091                           0.722222                           0.714286                           0.590909                           0.500000                           0.214286
11         Chelsea   Arsenal           2           2     X                    3.0               2.333333                    2.2                    1.0               0.666667                    1.0               11                  1              2014                    5                0.666667                0.791667                           0.909091                           0.888889                           0.928571                           0.590909                           0.277778                           0.428571
12         Chelsea   Arsenal           4           1     1                    2.0               2.666667                    2.2                    2.0               1.333333                    1.2               12                  1              2014                    6                1.000000                0.500000                           0.590909                           1.000000                           0.928571                           0.909091                           0.666667                           0.571429

编辑:iconv是必需的,因为此功能将替换包含重音符号的文本。

编辑2:示例:function highlight($search, $subject, $htmlTag = 'mark') { if (empty($search) === true) { return $subject; } $searchParts = explode(' ', str_replace("'", '', iconv('UTF-8', 'ASCII//TRANSLIT', $search))); $subjectParts = explode(' ', str_replace("'", '', iconv('UTF-8', 'ASCII//TRANSLIT', $subject))); $originalSubject = explode(' ', $subject); $result = []; foreach ($subjectParts as $row => $subjectPart) { foreach ($searchParts as $searchPart) { if (false !== $pos = stripos($subjectPart, $searchPart)) { $result[] = mb_substr($originalSubject[$row], 0, $pos) . '<' . $htmlTag . '>' . mb_substr($originalSubject[$row], $pos, mb_strlen($searchPart)) . '</' . $htmlTag . '>' . mb_substr($originalSubject[$row], $pos + mb_strlen($searchPart)); continue 2; } } $result[] = $originalSubject[$row]; } return implode(' ', $result); } 结果:“Satelitnýprijímač,Amiko HD815 5”

1 个答案:

答案 0 :(得分:1)

这就是我要做的:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div data-blah="asdf" style="border: 1px solid red;"></div>
<div data-blah="qwert" style="border: 1px solid green;"></div>

演示:jQuery's .data()