【问题标题】:Preg match all img tags except the src with .gif extensionPreg 匹配除 src 之外的所有 img 标签,扩展名为 .gif
【发布时间】:2015-07-08 03:08:21
【问题描述】:

我正在自定义一个插件名称 post-script-responsive-images ,我希望此插件不应该使用 .gif 扩展名重新调整图像大小,因为在调整 .gif 图像大小后,它们会失去其属性动画。

我看了插件,发现preg_match_all

preg_match_all('/<img[^>]+>/i',$content, $result); 

负责从内容区域获取所有图像标签。但我想排除 .gif 扩展名。 我怎样才能做到这一点。

【问题讨论】:

    标签: regex wordpress preg-match-all


    【解决方案1】:

    通过使用否定的lookahead

    /<img(?:(?!\.gif\b)[^>])+>/i
    

    (不检查源属性,只要在关闭&gt;之前没有.gif

    See test at regex101; 旁注: regex 不是解析 html 的手段

    【讨论】:

      猜你喜欢
      • 2012-12-20
      • 1970-01-01
      • 1970-01-01
      • 2015-12-07
      • 1970-01-01
      • 2014-09-14
      • 1970-01-01
      • 1970-01-01
      • 2019-08-15
      相关资源
      最近更新 更多