Ben Godfrey

Search term highlighting

I added a regular expression to highlight search terms in matching entries, in the style of Google’s cache. This is a nice thing, if a bit poncey. It has a known issue that it will match text in a tag.

E.g. a search for hypothetical will replace all instances of hypothetical with <span style="background-color:#9f0;">hypothetical</span>. If there is a link to hype it will go from:

<a href="http://hypothetical.co.uk/">hype!</a>

to

<a href="http://<span style="background-color:#9f0;">hypothetical</span>.co.uk/">hype!</a>

Which isn’t valid HTML and will snap browsers like a twig. Well, nearly.

Comments

Ben Godfrey http://aftnn.org

Solved it!

/(>[^<]*?)($alt_words)([^<]*?<)/si

instead of just

/($alt_words)/si

It was really much simpler than I was expecting. Lateral thinking wins again!

Anyway, now I have the problem that if you search for something the 'read/permalink' and probably the 'not yet' links don't work. Hint: you changed $base remember.

Cancel

Comments are closed for this post.

Add a new comment

Comments are closed for this post.