views:

66

answers:

2

Hi,

I wrote a little search script for a client, it works and words get highlited, BUT...

Imagine this situation:

search term: test

found result: Hello this <a href="/news/this-is-a-test">is a test</a>

In this example both 'test' in the href part and between the <a> tags get highlited, breaking the link.

How could I prevent this?

Edit:

So this is what I need: A regex replace function that replaces all matched search strings EXCEPT the ones that are located inside a href attribute

Greets, nick

+1  A: 

You can not parse XML with regular expressions. :( If you want a dirty regex solution that still works in many cases you may try this regex.

">[^<]*?(test)"

First you look for a tag closing brace and than you make sure that no other tag is opened in between.

Ideally you want to parse HTML and replace only the textual parts of it.

Muxecoid
Yes indeed, I need to parse HTML and replace the textual parts only.>[^<]*?(test) is the best solution then?
Bundy
A: 

Got it!

$body = $row['body'];
$pattern = "/".$search_string."(?!([^<]+)?>)/i";
$replacement = "<strong class='highlite'>".$search_string."</strong>";
$altered_body = preg_replace($pattern, $replacement, $body);

print($altered_body);
Bundy