A better example would be this:
$string = "That is a very nice ford mustang, if only every other ford was quite as nice as this honda";
I want to replace the car names with a link for manufacturer and model if they match, or just manufacturer, but if there is a make and model it puts links within links if you use str replace for example….
$remove = array("ford mustang","ford","honda");
$replaceWith = array("<a href='fordID'>ford</a>","<a href='fordmustangID'>ford mustang</a>","<a href='hondaID'>honda</a>");
This gives the result:
"That is a very nice <a href='<a href='fordmustangID'>ford mustang</a>ID'><a href='fordmustangID'>ford mustang</a></a>, if only every other <a href='fordmustangID'>ford mustang</a> was quite as nice as this <a href='hondaID'>honda</a>"
I only want it to create a hyperlink if there isn’t one already like this:
"That is a very nice <a href='fordmustangID'>ford mustang</a>, if only every other <a href='fordID'>ford</a> was quite as nice as this <a href='hondaID'>honda</a>"
edit:
Took me a good while, but here’s what I came up with:
Pass it an array such as the one you were talking about:
and a string:
It gives precedence to larger string keys, so if you have
fordandford mustang, it will replaceford mustangwith the link.Not very practical, but might work.
I used regular expressions with negative lookaheads and lookbehinds to make sure the portion of the string we’re replacing isn’t part of an alphanumeric sequence (like
12ford23orafford) or touching the start or end tag of an element.