2016-11-18 38 views
0
<div class="media_embed" height="qqq" width="www"> 
<a href="http://giphy.com/gifs/dancing">via GIPHY</a> 
<div class="media_embed" height="eee" width="rrr"> 
<a href="http://giphy.com/gifs/movie">via GIPHY</a> 

我想这样做: 我不得不从锚标记得到德href并把它bewtween img标签,并添加figure标签如何使用正则表达式在php中用“img”代替“a”标签?

<figure> 
    <img src="http://giphy.com/gifs/dancing" /> 
</figure> 
<figure> 
    <img src="http://giphy.com/gifs/movie" /> 
</figure> 

可以请你帮我

+0

不要使用正则表达式,使用解析器。 – chris85

+0

请标记我的答案完成此讨论。 – Mohammad

回答

0

的最佳解决方案解析html然后改变它。您可以使用DOMDocument解析html。但是,如果您想使用正则表达式,请使用preg_replace()a标记替换为另一个标记。

echo preg_replace("/<a href=\"(.*)\">.*<\/a>/", "<figure><img src='$1' /></figure>", $html) 

检查结果demo

相关问题