2016-11-18 52 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

相關問題