var sidebar_align = 'right';
var content_container_margin = parseInt('350px');
var sidebar_width = parseInt('330px');
Highlight words (PHP)
I have a few ideas but i need something a lot better so please help.
I am trying to highlight all the words that match a given string.
i want something like this,
then it should search the yahoo.com's page and highlight all the "blahblah" words.
Any idea how should i do that or has it been coded before ?
A clumsy and naive way of doing it would be:
This is probably a bad idea on any sort of marked-up text, as it'll clobber parameter names and such as well. You need a state machine to fix that.
$new = preg_replace("/\b($word)\b/i", "<span class='highlight'>\1</span>", $string);
Last edited by error404; 12-14-2005 at 01:22 AM.
thanks for the reply,
that's what i have in mind but how do you grab words from a given url and "highlight" them ?
If you wanted to get the words from the url, such as 'highlight.php?highlight=word', you might try doing:
Put that before the preg_replace(); that error404 wrote.
$word = $_GET[highlight];
You might want to do some checks on that as-well.
I hope that helps , if that's not you wanted, I apoligise,
- Dark Light.
Note that the \1 in my code should be a double backslash (or $1 instead if you're using a non-archaic PHP version).