我需要用几个数据库条目替换所有/ media / * url {{media url = URL}}
我有以下测试:
<img src="/media/wysiwyg/nutritionfacts/NLAHCLEA00600000CP.jpg" alt="NLA for Her Her Cleanse 60ct" />
<img src="/wysiwyg/nutritionfacts/NLAHCLEA00600000CP.jpg" alt="NLA for Her Her Cleanse 60ct" />
<img src="/nutritionfacts/NLAHCLEA00600000CP.jpg" alt="NLA for Her Her Cleanse 60ct" />
<img src="/media/wysiwyg/nutritionfacts/NLAHCLEA00600000CP.jpg" alt="NLA for Her Her Cleanse 60ct" />
我试图使用以下正则表达式
<img.+?src=\"\/media(.+?)[\"'].*?>/g
最终结果应该是:
<img src="{{media url="/wysiwyg/nutritionfacts/NLAHCLEA00600000CP.jpg}}" alt="NLA for Her Her Cleanse 60ct" />
我打算使用php的preg_replace
我应该如何使用正则表达式来查找和替换网址?我试图在这里弄明白:https://regex101.com/r/kF3gI1/1
答案 0 :(得分:1)
所以我继续并假设所有条目都将使用双引号来包含图像URL。这是我用来测试的代码:
$tests = array(
'<img src="/media/wysiwyg/nutritionfacts/NLAHCLEA00600000CP.jpg" alt="NLA for Her Her Cleanse 60ct" />',
'<img src="/wysiwyg/nutritionfacts/NLAHCLEA00600000CP.jpg" alt="NLA for Her Her Cleanse 60ct" />',
'<img src="/nutritionfacts/NLAHCLEA00600000CP.jpg" alt="NLA for Her Her Cleanse 60ct" />',
'<img src="/media/wysiwyg/nutritionfacts/NLAHCLEA00600000CP.jpg" alt="NLA for Her Her Cleanse 60ct" />',
);
foreach ($tests as $str) {
echo preg_replace("/<img.+?src=\"(.+?)\"/","<img src=\"{{media url=\"$1\"}}\"",$str)."\r\n";
}
以下是结果:
<img src="{{media url="/wysiwyg/nutritionfacts/NLAHCLEA00600000CP.jpg"}}" alt="NLA for Her Her Cleanse 60ct" />
<img src="/wysiwyg/nutritionfacts/NLAHCLEA00600000CP.jpg" alt="NLA for Her Her Cleanse 60ct" />
<img src="/nutritionfacts/NLAHCLEA00600000CP.jpg" alt="NLA for Her Her Cleanse 60ct" />
<img src="{{media url="/wysiwyg/nutritionfacts/NLAHCLEA00600000CP.jpg"}}" alt="NLA for Her Her Cleanse 60ct" />