Forum Moderators: coopster

Message Too Old, No Replies

Regex help with search function

         

hafnius

4:00 pm on Sep 26, 2004 (gmt 0)

10+ Year Member



We are trying to make a search function but got a bit stuck when it came too Regex. What we got is.

$concat = preg_replace("/($word)/i","<strong class=\"searchWordFound\">$1</strong>",$concat);

We need to convert these to a Regex that does not search for $word [i]inside[/] HTML tags or their attributes.

The reason for this is that if I do a search for ex “a e” when the function does the second round of the above preg_replace it will add a new <strong> tag around the e inside searchWordFound. Same with “a o”, “e a” etc.

Is that doable at all?
/Hafnius

dcrombie

4:22 pm on Sep 26, 2004 (gmt 0)



Your best bet is probably the DOM functions but it can be complicated. You can do something similar in a JavaScript that runs after the page is loaded.

mincklerstraat

6:08 pm on Sep 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



try this maybe:
$concat = preg_replace("/>([^<]*)($word)([^<]*<)/i","$1<strong class=\"searchWordFound\">$2</strong>$3",$concat);
Needs to be beween a > and a <, like eg. between <html> and </html> or <body> and </body> - if not, just add a > and a <, and then get rid of 'em again.