【问题标题】:String replace with regex in PHP在PHP中用正则表达式替换字符串
【发布时间】:2013-06-19 08:20:06
【问题描述】:

我想用php修改一个html文件的内容。 我正在将样式应用于img标签,我需要检查标签是否已经具有样式属性,如果有,我想用我自己的替换它。

$pos = strpos($theData, "src=\"".$src."\" style=");
    if (!$pos){
        $theData = str_replace("src=\"".$src."\"", "src=\"".$src."\" style=\"width:".$width."px\"", $theData);
    }
    else{
        $theData = preg_replace("src=\"".$src."\" style=/\"[^\"]+\"/", "src=\"".$src."\" style=\"width: ".$width."px\"", $theData);
    }

$theData 是我收到的 html 源代码。 如果没有找到样式属性,我成功插入了自己的样式,但我认为问题在于已经定义了样式属性,因此我的正则表达式不起作用。

我想用我的新样式属性替换其中的所有样式属性。 我的正则表达式应该是什么样子?

【问题讨论】:

    标签: php html regex string


    【解决方案1】:

    您应该使用 DOM 解析器,而不是使用正则表达式。

    使用DOMDocument的示例:

    <?php
    $html = '<img src="http://example.com/image.jpg" width=""/><img src="http://example.com/image.jpg"/>';
    
    libxml_use_internal_errors(true);
    $dom = new DOMDocument();
    $dom->loadHTML('<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />'.$html);
    $dom->formatOutput = true;
    
    foreach ($dom->getElementsByTagName('img') as $item)
    {
        //Remove width attr if its there
        $item->removeAttribute('width');
    
        //Get the sytle attr if its there
        $style = $item->getAttribute('style');
    
        //Set style appending existing style if necessary, 123px could be your $width var
        $item->setAttribute('style','width:123px;'.$style);
    }
    //remove unwanted doctype ect
    $ret = preg_replace('~<(?:!DOCTYPE|/?(?:html|body|head))[^>]*>\s*~i', '', $dom->saveHTML());
    echo trim(str_replace('<meta http-equiv="Content-Type" content="text/html;charset=utf-8">','',$ret));
    
    //<img src="http://example.com/image.jpg" style="width:123px;">
    //<img src="http://example.com/image.jpg" style="width:123px;">
    
    ?>
    

    【讨论】:

    • 我从未使用过 DOM 解析器,与我目前使用的 string_replace 设置相比,使用它有什么优势?
    • 简单地放置 y̷̙o҉S҉S҉S҉S҉S҉S҉S҉S҉H҉L͎̫̗͚͖͙̪͆̔̊ͭͩ̃d̑͢d̑͢d̑͢e̒ͩͣ̅̓͒̀ͤ̂͂̄̊҉̶̝̗̦͡͠ƞɛv̈́̂̈́e̶e̶r̘̝̙ͤ͂̾̆p̯͍̭@r̘̝̙ͤ͂̾̆ $ ̝ͤ͂̾̆eͧ̾ͬ͛ͪ̈́ ͧ̾ͬ͛ͪ̈́ ͧ̾ͬ͛ͪ̈́ ͧ̾ͬ͛ͪ̈́͞h̸̡̪̯ͨ͊̽̅̾̎7mͧ̾ͬl͎̫̗͚͖͙̪͆̔̊ͭͩ̃l͎̫̗͚͖͙̪͆̔̊ͭͩ̃imͧ̾ͬl͎̫̗͚͖͙̪͆̔̊ͭͩ̃imͧ̾ͬi̍̈́̂̈́i̍̈́̂̈́ẗ́̂̈́h҉有很多领域会导致您的正则表达式失败。 stackoverflow.com/questions/1732348/…
    • 详细说明,在您选择的答案中,如果样式标签位于 src 之前或属性" style=" 之间有额外的空格会发生什么。
    • 好吧,这是有道理的。感谢您澄清这一点
    【解决方案2】:

    这里是解决这个问题的正则表达式变体:

    <?php
    $theData = "<img src=\"/image.png\" style=\"lol\">";
    $src = "/image.png";
    $width = 10;
    
    //you must escape potential special characters in $src, 
    //before using it in regexp
    $regexp_src = preg_quote($src, "/");
    
    $theData = preg_replace(
        '/src="'. $regexp_src .'" style=".*?"/i',
        'src="'. $src .'" style="width: '. $width . 'px;"',
        $theData);
    
    print $theData;
    

    打印:

    <img src="/image.png" style="width: 10px;">
    

    【讨论】:

      【解决方案3】:

      正则表达式:

      (<[^>]*)style\s*=\s*('|")[^\2]*?\2([^>]*>)
      

      用法:

      $1$3
      

      示例:

      http://rubular.com/r/28tCIMHs50

      【讨论】:

        【解决方案4】:

        搜索:

        &lt;img([^&gt;]<em>)style="([^"]</em>)"

        并替换为:

        &lt;img\1style="attribute1: value1; attribute2: value2;"

        http://regex101.com/r/zP2tV9

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-13
          • 2016-03-29
          • 1970-01-01
          • 2015-11-30
          • 2021-11-29
          相关资源
          最近更新 更多