code_junkie

how search for a substring found within a string of text and highlight all of it

Nov 12th, 2011
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. <div><span style="background: #f00">My hair cut so cool!</span></div>
  2.  
  3. 0:
  4. 1: $term = 'cool';
  5. 2: ob_start();
  6. 3:
  7.  
  8. 10: foreach($items as $item) {
  9. 11: // echoing results here
  10. 12: echo '<div>' . $item->text . '</div>';
  11. 13: }
  12.  
  13. 30: $content = ob_get_contents();
  14. 31: ob_clean() ;
  15. 32:
  16. 33: $pattern = "/(?<!<[^>])($term)/i";
  17. 34: $replace = "<span style="background: #f00">$1</span>";
  18. 35: echo preg_replace($pattern, $replace, $content);
  19. 36:
  20.  
  21. $pattern = "/[^<>]*$term[^<>]*/i";
  22. $replace = "<span style="background: #f00">$0</span>";
Add Comment
Please, Sign In to add comment