Jump to content

Highlight class for search keywords within HTML string


spaze

Recommended Posts

Hello all!

 

I am writing a class where you set a string to be searched and a keyword to search.

 

I have written so far:

 

$replacement = '<' . $this->_htmlTag . ' class="' . $this->_htmlClass . '">' . $keyword . '</' . $this->_htmlTag . '>';

 

$result = preg_replace("/(>|^)([^<]+)(?=<|$)/iesx","'\\1' . str_replace('" . $keyword . "', '" . $replacement . "', '\\2')", $entry);

 

This works fine as long as the search is case sensitive, It wraps found instances if they are not inside HTML tags.

 

I can't seem to figure out how to make case insensitive replacement and I knwo why. It's because I wrap the search keywords with html tags and not the found instances, so can someone please show me how to complete this code to replace the found instances instead of using the str_replace?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.