XOOPS  2.6.0
Highlighter.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  */
11 
12 namespace Xmf;
13 
27 {
41  public static function apply($words, $body, $pre = '<strong>', $post = '</strong>')
42  {
43  if (!is_array($words)) {
44  $words=str_replace(' ', ' ', $words);
45  $words=explode(' ', $words);
46  }
47  foreach ($words as $word) {
48  $body=Highlighter::splitOnTag($word, $body, $pre, $post);
49  }
50 
51  return $body;
52  }
53 
64  private static function splitOnTag($needle, $haystack, $pre, $post)
65  {
66  return preg_replace_callback(
67  '#((?:(?!<[/a-z]).)*)([^>]*>|$)#si',
68  function ($capture) use ($needle, $pre, $post) {
69  $haystack=$capture[1];
70  $p1=mb_stripos($haystack, $needle);
71  $l1=mb_strlen($needle);
72  $ret='';
73  while ($p1!==false) {
74  $ret .= mb_substr($haystack, 0, $p1) . $pre
75  . mb_substr($haystack, $p1, $l1) . $post;
76  $haystack=mb_substr($haystack, $p1+$l1);
77  $p1=mb_stripos($haystack, $needle);
78  }
79  $ret.=$haystack.$capture[2];
80 
81  return $ret;
82 
83  },
84  $haystack
85  );
86  }
87 }
static apply($words, $body, $pre= '< strong >', $post= '</strong >')
Definition: Highlighter.php:41
static splitOnTag($needle, $haystack, $pre, $post)
Definition: Highlighter.php:64