0

So, I can easily do this:

$keywords = array(' jacket ',' sweater ',);
$urls = array(' <a href="#">jacket</a> ',' <a href="#">sweater</a> ',);
$content = str_ireplace($keywords,$urls,$content);

But the problem is when something like jacket or sweater is already in a link tag.

I can't think of any simple solutions to this...

dismalist
  • 3
  • 1

2 Answers2

0
  • step 1) find all instances of each keyword which are within a url & replace them with a GUID
  • step 2) replace all other instances of each keyword with thier replacement
  • step 3) replace the GUID's with their origional keywords.

Hope you choose your Guid's and how you do your searching is up to you :)

GreyCloud
  • 3,030
  • 5
  • 32
  • 47
0

You might be able to construct a fancy regex for this, but it would be extremely complicated and nearly impossible to maintain. IMO, you should take a token-based approach to solve this problem. I have a project called Lexentity that we use to replace apostrophes and quotes, etc., with their numerical entities. You could probably adapt that code for your purposes. There's also a HTMLTokenizer (and HTMLTokenset) in Habari that you might find useful.

scoates
  • 853
  • 7
  • 11