转换<a> tag to <span> if href contains something and remove href

时间:2016-08-18 12:39:41

标签: php html regex replace

Hi I'm using wikipedia api and images come with html output. But don't want to link images to wikipedia, i just want to show them. So I want to convert <a> tags to span or div which they are for images. Also href must be deleted How can I do it with php?

Input

<a href="/wiki/Dosya:House_in_Cappadocia_22.jpg" class="image">
    ...(img tag, captions etc)
</a>

Output should be (if href contains "Dosya");

<span class="image">
     ...(img tag, captions etc)
</span>

1 个答案:

答案 0 :(得分:1)

假设:您有一个包含此内容的字符串

$string = '<a href="/wiki/Dosya:House_in_Cappadocia_22.jpg" class="image">  ...(img tag, captions etc) </a>';

在此处添加代码

preg_match('/<a href="(.*)" class="(.*)"/', $string, $matches);
if($matches[2] == 'image'){
     //set image code based on $matches[1] which is source of image or link
}
相关问题