【问题标题】:Regex to remove quotes around attributes when possible正则表达式尽可能删除属性周围的引号
【发布时间】:2015-10-15 20:59:14
【问题描述】:

我搜索一个使用 PHP 的正则表达式来清理(和缩小)我的 HTML 代码。

这是一个 HTML 标签的例子:

<meta name="viewport" content="width=device-width, initial-scale=1" >

还有异常结果:

<meta name=viewport content="width=device-width, initial-scale=1" >

另一个例子:

<img src="picture.png" alt="Picture" width="800" height="20"/>

还有异常结果:

<img src="picture.png" alt=Picture width=800 height=20/>

我已经在使用这个类了:https://github.com/mrclay/minify/blob/master/lib/Minify/HTML.php 但是缺少我想要的功能。

谢谢:)

【问题讨论】:

    标签: php html regex


    【解决方案1】:

    使用这个正则表达式:

    "([^"=.]+?)"

    并替换为$1

    Regex live here.

    基本意思是:

    "                # start quote character
    (                # start group
        [^"=.]+?     # if the content between these quotes contains 
                         # equal or dot characters.. then don't match
    )                # end group
    "                # end quote character
    

    【讨论】:

      猜你喜欢
      • 2017-07-09
      • 2020-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-16
      相关资源
      最近更新 更多