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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.