一个更好的例子是这样的:
这将给出以下结果:
$string = "That is a very nice ford mustang, if only every other ford was quite as nice as this honda";
如果汽车名称匹配,我希望您能用制造商和型号的链接替换它们,或者只使用制造商,但如果有品牌和型号,则会在链接内放置链接(例如,如果使用str replace)...
$remove = array("ford mustang","ford","honda");
$replaceWith = array("<a href='fordID'>ford</a>","<a href='fordmustangID'>ford mustang</a>","<a href='hondaID'>honda</a>");
这将给出以下结果:
"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>"
如果还没有超链接,我只希望它创建一个超链接,就像这样:
"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>"
preg_replace
并迭代每个$remove
。负向先行断言应该可以解决问题。 - Josh