CodeIgniter's highlight_phrase() function, in the Text Helper library, looks like this:
/**
* Phrase Highlighter
*
* Highlights a phrase within a text string
*
* @access public
* @param string the text string
* @param string the phrase you'd like to highlight
* @param string the openging tag to precede the phrase with
* @param string the closing tag to end the phrase with
* @return string
*/
if ( ! function_exists('highlight_phrase'))
{
function highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>')
{
if ($str == '')
{
return '';
}
if ($phrase != '')
{
return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str);
}
return $str;
}
}
However, the string I need to run it on is already fully-formatted HTML. What modification can I make to the preg_replace() so that it will not do replacements inside < and > tags. For example, if I were to pass in <a href="http://www.daniweb.com">Foo</a>
as the string, and daniweb
as the phrase to be wrapped, I don't want it to turn it into <a href="http://www.<strong>daniweb</strong>.com">Foo</a>
. It should only work on text between > and <.