【问题标题】:Convert <a> tag to <span> if href contains something and remove href如果 href 包含某些内容,则将 <a> 标记转换为 <span> 并删除 href
【发布时间】:2016-08-18 12:39:40
【问题描述】:

您好,我使用的是 wikipedia api,图像带有 html 输出。但不想将图像链接到维基百科,我只想展示它们。所以我想将&lt;a&gt; 标签转换为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>

【问题讨论】:

  • 用 RegEx 解析 HTML ...我觉得 那个 答案的链接即将出现...

标签: php html regex replace


【解决方案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
}

【讨论】:

    猜你喜欢
    • 2011-05-18
    • 2015-07-12
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 2013-09-05
    相关资源
    最近更新 更多