0

Possible Duplicate:
Highlight keywords in a paragraph

I have been struggling with the idea of highlighting search terms in html or pure text strings. I have seen many examples that just don't work so here is my crack at it. Please give me some cases that this will break the html or won't work. The only two case that I can come up with are: if there is a > within the text string such as, "I am > than a computer" and ill formatted html. So here's the code. Thank you and I hope it helps someone

function search_highlight($text,$search_terms)
{   
  $color = array('#FFFF00','#00FFFF','#FF9900','#00FF00','#CCCCCC','red','grean','orange');
  $count = 0;

  if ( ! is_array($search_terms))
  {
    $search_terms = explode(' ', $search_terms);
  }

  // Highlight each of the terms
  foreach ($search_terms as $term)
  {
    //skip blank terms that arise from double spaces
    if( ! $term) continue;

    //Regex Explained:
    //The regex first matches a word boundary
    //Next it matches the search term liternal
    //Next it matches a word boundary
    //Next is matches, but doesn't include the following regex...
    //Anything other than the literals < and > zero or more times
    //Next it will match either a < literal or the end of the string
    $text = preg_replace('/\b('.preg_quote(trim($term)).')\b(?=[^><]*<|.$)/i', '<span    style="background-color:'.$color[$count].'">\1</span>', $text);

    //increment counter for new color
   $count++;
 }
 return $text;
}
Community
  • 1
  • 1
John
  • 311
  • 1
  • 10
  • 1
    the reason why all of them probably dont work is because you want to do that with DOM and not with Regex – Gordon Jan 24 '12 at 15:25
  • Do you use javascript to traverse through the DOM? If so have any plugins that you would suggest? I have only seen one php DOM parser and it is quite slow. – John Jan 24 '12 at 15:30
  • either javascript or http://php.net/dom – Gordon Jan 24 '12 at 15:32
  • Makes me wonder how they create construct the DOM tree..regex maybe? – John Jan 24 '12 at 15:37
  • @Gordon: it is a dupe and the other question has a very good [answer](http://stackoverflow.com/a/4081567/911182) IMO. – Herbert Jan 24 '12 at 16:08

1 Answers1

2

I would use the DOMDocument class for this, and simply do a plain text replace on the node contents instead of trying to come up with a ninja regex.

FtDRbwLXw6
  • 27,774
  • 13
  • 70
  • 107
  • well based on Gordon's comments and your comment I am going to go with the DOM approach, but use javascript so I can save some delivery time. Thank you for your insight. – John Jan 24 '12 at 18:19