Tuesday, 13 August 2013

Modify regex in a script that highlights certain words in a text by adding span classes to them

Modify regex in a script that highlights certain words in a text by adding
span classes to them

I am working on a code that highlights certain words, using a regex.
Here it is:
function addRegEx($word){
return "/\b(\w+)?".$word."(\w+)?\b/i";
}
function highlight($word){
return "<span class=\"highlighted\">".$word[0]."</span>";
}
function customHighlights($searchString,$toHighlight){
$searchFor = array_map('addRegEx',$toHighlight);
$result =
preg_replace_callback($searchFor,'highlight',$searchString);
return $result;
}
Lets say I use the function customHighlights to searc for the word "car"
in a certain text:
Using the boundary - \b - method, the script searches for the word car in
the text.
In the regex, I have added (\w+)? in front and after the word, so the
script would match words that contain "car" - cars, sportcars, etc...
The problem is, it messes up the inner html, for example:
This is a great car. Click here for <a href="http://cars.com">more</a>
The script will match the word car in the url of the link above, adding
span classes to it and messing up the html.
How would you modify the regex and avoid this?

No comments:

Post a Comment