Forum Moderators: coopster

Message Too Old, No Replies

Bolding keywords with preg replace

         

asantos

5:13 pm on Dec 26, 2006 (gmt 0)

10+ Year Member



Im setting up a search engine for a content site. It works find, but i still need the keywords to be BOLD on the search results.

For example, if i search for "dog" and one of the results is:

* Dog contest winner
Last friday an Akita dog won the dog contest...

The result SHOULD come up as:

* Dog contest winner
Last friday an Akita (STRONG)dog(/STRONG) won the dog contest...

--I dont quite get the reg.expressions... but i know that this could be accomplished with preg_replace. I would really appreciate some help in here.

Andres

henry0

5:51 pm on Dec 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since your keyowrds type and keywords returned number are never going to be the same.
You first need to "gather" all returnded keywords
and loop through to perform a preg_replace [us3.php.net]
The manual is real clear on using that function.

eelixduppy

6:00 pm on Dec 26, 2006 (gmt 0)



It would look something like this:

$pattern = "/(".$keyword.")/i";
$replace = "<strong>\\1</strong>";
echo preg_replace($pattern,$replace,$string);

Again you may want to make sure that it is only the keyword by itself, and not the keyword within another word.

asantos

6:28 pm on Dec 26, 2006 (gmt 0)

10+ Year Member



thanks eelixduppy, that works just fine!