Reputation: 63
I have an script which gets the content of webpage by cURL, Now i have to highlight some keywords in that. str_replace is not working beacuse if i replace 'a', than all will be replaced and that will create a problem.
Please can anybody help?
Upvotes: 0
Views: 227
Reputation: 20475
Personally I would only handle this on the client side, for one reason only, don't reinvent the wheel or mess around if you don't need to.
I did exactly what you are asking by utilizing a plugin for jquery:
JavaScript text higlighting jQuery plugin
What you do is basically in the <head>
of your HTML create an entry like:
$('body').removeHighlight().highlight('<?php echo "text to highlight"; ?>');
Easy as pie, and don't have to tinker with anything
Upvotes: 0
Reputation: 285
If you want to highlight words you can use the *preg_replace* function like that :
foreach($myKeywords as $keyword)
$myNewCode = preg_replace("/[\\s*\\.,](".$keyword.")[\\s*\\.,]/",'<span class="highlight">${1}</span>',$myCode);
Regards
Upvotes: 1
Reputation: 222278
You can replace ' a '
instead of just 'a'
if you want to use str_replace.
Upvotes: 0