我对正则表达式一无所知,也没有找到一个清晰的解决方案。
我想要搜索并替换一些文本($content)中锚点href内的任何url为新的url(保存在变量$newurl中)。
将此更改为:
<a href="http://blogurl.com/files/foobar.jpg"><img alt="foobar" src="http://blogurl.com/files/2011/03/foobar_thumb.jpg" /></a>
To this:
<a href="http://newurl.com/here/"><img alt="foobar" src="http://blogurl.com/files/2011/03/foobar_thumb.jpg" /></a>
我认为在这种情况下使用preg_replace最好。类似这样:
preg_replace('Look for href="any-url"',
'href="$newurl"',$content);
这个想法是让 WordPress 前端页面上的所有图片链接到它们的文章而不是全尺寸图片(这是它们默认的方式)。通常只需要替换一个 url,但我认为替换所有可能的匹配也没有问题。
希望以上内容能够清楚表述,提前感谢!