我必须更换数千个链接到图像托管的Photobucket,并改变链接到一个不同的托管网站。这些链接包含在Wordpress站点的帖子中。也就是说,我必须找到以下语法:
<div style="clear: both; text-align: center;"><a href="http://photobucket.com/albums/mypath/IMAGENAME.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="photobucket.com/albums/mypath/IMAGENAME.jpg" /></a></div>其中mypath可以变化,并将整个DIV标记替换为
<img class="alignnone size-full aligncenter" src="http://mynewpath/IMAGENAME.jpg" alt="" />我找到了WordPress的Search Regex plugin,它允许使用RegEx的大量搜索和替换。不幸的是,我不能创建一个足够具体的RegEx来只识别我想要的DIV标签。我想出了这个:
<div\b[^>]*>.*?<img.*?photobucket.*?\/(.*?\.jpg).*?<\/div>这在大多数情况下都是有效的。但是,它有时会捕获连续的DIV标记对。如何确保捕获单个DIV标记并捕获其中的图像名称?
PS:我知道不推荐使用Regex解析HTML,但搜索Regex插件是我找到的解决问题的最快方法。
编辑:为清晰起见,编辑了示例
发布于 2017-07-24 21:23:21
你可以使用jQuery来帮你做到这一点。因为您不能访问SQL来进行搜索和替换。
假设你有一个类似于http://replaceThis.com/somethingSomething WIth next jQuery的网址,你可以替换该网址的所有实例:
$('a[href^="http://replaceThis.com"]').each(function(){
var thisHref = $(this).attr('href');
thisHref.replace('replaceThis','withThis');
});编辑:
你可以使用http://api.jquery.com/unwrap/,你可以检查这个元素是否有父div,以及它是否有解包。
https://stackoverflow.com/questions/45280493
复制相似问题