0

I currently have a link replacement function that I wrote up that finds certain keywords and turns said key words into links to related content.

It has been working fairly well so far, except on occasion, a link that it adds in also contains a keyword.

This issue has resulted in my keywords being turned into a mess of hmtl code and unworking links wherever this occurrence happens.

What I plan to do is one final check before the replace to verify if the keyword is within the element .

Is it possible to determine if the content is between those tags? If so, how is it done?

These are the current patterns that I am using to find the keywords.

$pattern = "/\b $kw \b/";
$pattern2 = "/\b $kw. \b/";
$pattern3 = "/\b $kw, \b/";
Zach Harner
  • 55
  • 10

1 Answers1

1

Firstly your patterns can be merged into one:

$pattern = "/\b $kw[.,]? \b/";

I question the whitespaces there. You probably don't want them.

$pattern = "/\b$kw[.,]?\b/";

And now for VOODOO BLACK MAGIC OF REGEX DOOM!

$pattern = "/<a\b.*?<\/a>(*SKIP)(*FAIL)|\b$kw[.,]?\b/";

But beware THE PONY and consider using a parser instead.

Community
  • 1
  • 1
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Thank you for your reply. I had just responded as it wasn't working, but I was also trying to use it as a conditional instead of just putting it in the preg replace. It now works perfectly. I heard that the regex is not the way to go for modifying html, but this project is small and only allows my admins to enter info, so if anything goes haywire, we'll know right away. Thank you again! – Zach Harner Sep 10 '14 at 15:57