转换& lt; a>标签为<span>如果href包含某些内容,删除href


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

嗨,我正在使用维基百科api和图像与html输出。但我不想把图片链接到维基百科,我只是想展示它们。所以我想把<a>标签转换成spandiv它们是用于图像的。也必须删除href我怎么能做到这一点与php?

输入

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

输出应为(如果href包含"Dosya");

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

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

$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
}