我需要帮助转动这样的东西:
<img class="coup-pic" src="http://example.com/img/pic.jg" alt="" />
为:
<a href="http://example.com/img/pic.jg"><img class="coup-pic" src="http://example.com/img/pic.jg" alt="" /></a>
我需要能够动态搜索一个字符串,就像上面使用PHP和包装它在一个标签与src属性用作标签的标签。我想我需要使用preg_replace,但我不知道正确的正则表达式使用。
谢谢你的帮助!
我不是目前的地方,我可以测试这个,但给这个尝试:
$html = preg_replace('/(<img [^>]*src="([^"]*)"[^>]*>)/i', '<a href="$2">$1</a>', $html);
这似乎可以工作:
preg_replace('/'<img class="([a-zA-Z0-9'-]*)" src="([A-Za-z0-9'-'.':'/]*)" alt=""'/'>/','<a href="''2"><img class="''1" src="''2" alt=""/></a>',$string);