Ayush
Ayush

Reputation: 63

PHP Text highlighter

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

Answers (5)

Jakub
Jakub

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

http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html

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

Nirav
Nirav

Reputation: 5760

use preg_rplace function......

Upvotes: 0

NiL
NiL

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

Dogbert
Dogbert

Reputation: 222278

You can replace ' a ' instead of just 'a' if you want to use str_replace.

Upvotes: 0

Jon
Jon

Reputation: 437534

If you want something fully customizable and with lots of features, GeSHi has you covered.

Upvotes: 0

Related Questions