0

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...

2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by blocblue
0

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);
}
0

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.

0

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);
}
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.