代码之家  ›  专栏  ›  技术社区  ›  yoda

PCRE替换标记样式

  •  1
  • yoda  · 技术社区  · 14 年前

    <div style="width:200px;"></div>
    

    <div style="width:100px;"></div>
    

    我现在所拥有的,由 simplehtmldom 是纯文本中的样式内容,如下所示:

    width:200px;
    

    如何匹配CSS属性并用PHP中的新值替换它?

    干杯!

    2 回复  |  直到 13 年前
        1
  •  2
  •   Tim Pietzcker    14 年前
    ([^\s:]+)[\s:]+([^:;]+)
    

    将冒号周围的值提取到反向引用1和2中。

    ([^\s:]+)[\s:]+(\d+)(\w+)
    

    if (preg_match('/([^\s:]+)[\s:]+(\d+)(\w+/', $subject, $regs)) {
        $attribute = $regs[1];
        $value = $regs[2];
        $unit = $regs[3];
    } else {
        // no match
    }
    
        2
  •  -2
  •   Sjoerd    14 年前
    preg_replace('~width:200px~', 'width:100px', $subject);