I have been trying to figure out how to perform a search and replace in php to create links within the text of paragraphs without having it interfere with any naming conventions in HTML codes, styles, names, and ids.

I was considering preg_match or preg_replace, but I am the worst with regular expressions. Any assistance would be appreciated.

8 Years
Discussion Span
Last Post by EverWebby

This is example.

function highlight($str,$searchText) 
	return (eregi_replace($searchText, "<span 
class='highlight'>$searchText</span>", $str));
$str = 'This is beautiful';
$searchText = 'is';
echo highlight($str,$searchText);

Hope this helps.


The text I am going to replace is within the content area of the page which may include other tags include div, img, p, and span along with associated attributes. So, the replaced word, words, or phrases may be within the tag's attributes. By performing a search and replace using the methods previously mentioned, would undoubtably mangle the output.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.