We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,599 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

PHP Search and Replace problem

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
23 Hours
Discussion Span
11 Months Ago
Last Updated
4
Views
renjithdas
Newbie Poster
4 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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);
}
blocblue
Practically a Posting Shark
837 posts since Jan 2008
Reputation Points: 272
Solved Threads: 161
Skill Endorsements: 13

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.

renjithdas
Newbie Poster
4 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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);
}
blocblue
Practically a Posting Shark
837 posts since Jan 2008
Reputation Points: 272
Solved Threads: 161
Skill Endorsements: 13

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0683 seconds using 2.68MB