I am having problem with search and replace html content. The records coming from the database. The populated string contains html mark up and image name also. I am doing a search and replace it will affect the image also. Could any one help me to get rid of this issue.

For example

$str = 'The PHP development team revit would like to announce the immediate availability of a new PHP version.
The PDT project provides a PHP <img src="Images/Revit-Icon.jpg"> Development Tools framework for the Eclipse platform.
PHP is especially suited for Web development and can be embedded into HTML.
There are many development tools for PHP.';

$keyword = 'revit'
I want to replace all the revit substring with highlighted yellow color. When I am doing that will affect the Image also .
Images/%3Cspan%20class=%27highlight_important%27%3Erevit%3C/span%3E-Icon.jpg

Please help me its urgent...

Recommended Answers

All 3 Replies

There was a similar post topic quite recently, just with BB syntax instead. Try:

function highlight($string, $term)
{
    return preg_replace("/(?<!\<)(\b{$term}\b)(?!\>)/i", '<span style="color:#f00;">\\1</span>', $string);
}

Thank you for the quick reply.. really appreciate you...But code still not completely working what I expected. Still image is also replacing with the code. I need to keep the image as image.

Hmm... after a little more searching, I came across another regex here.

$string = 'The PHP development team revit would like to announce the immediate availability of a new PHP version. The PDT project provides a PHP <img src="Images/Revit-Icon.jpg"> Development Tools framework for the Eclipse platform. PHP is especially suited for Web development and can be embedded into HTML. There are many development tools for PHP.';
echo highlight($string, 'revit');

/**
 * Find and replace search term in specified string with highlight syntax.
 *
 * @param string $string
 * @param string $term
 * @return string
 */
function highlight($string, $term)
{
    return preg_replace("/({$term})(?!([^<]+)?>)/i", '<span style="color:#f00;">\\1</span>', $string);
}
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.