【问题标题】:Get img tag details using preg_replace [PHP]使用 preg_replace [PHP] 获取 img 标签详细信息
【发布时间】:2017-11-30 06:59:57
【问题描述】:

这是我的内容:

<p><img src="http://localhost/contents/uploads/2017/11/1.jpg" width="215" height="1515"></p>

这是我的 PHP 代码:

function convert_the_content($content){
    $content = preg_replace('/<p><img.+src=[\'"]([^\'"]+)[\'"].*>/i', "<p class=\"uploaded-img\"><img class=\"lazy-load\" data-src=\"$1\" /></p>", $content);
    return $content;
}

我使用我的代码为&lt;p&gt; 标签和&lt;img&gt; 标签添加一个类,并将src="" 转换为data-src=""

我的代码已经从&lt;img&gt; 标记中删除了宽度和高度 attr 的问题,所以我的问题是如何更改我的代码以使其工作并获得这些详细信息?

注意:我的内容可能有很多 &lt;img&gt;&lt;p&gt; 标签。

【问题讨论】:

    标签: php html regex


    【解决方案1】:

    如果你只有这个非常精确的 HTML sn-p,你可以这样做更简单

    $html = <<< HTML
    <p><img src="http://localhost/contents/uploads/2017/11/1.jpg" width="215" height="1515"></p>
    HTML;
    
    $html = str_replace('<p>', '<p class="foo">', $html);
    $html = str_replace(' src=', ' data-src=', $html);
    echo $html;
    

    这将输出

    <p class="foo"><img data-src="http://localhost/contents/uploads/2017/11/1.jpg" width="215" height="1515"></p>
    

    如果您尝试转换任意 HTML,请改用consider using a DOM Parser

    <?php
    $html = <<< HTML
    <html>
      <body>
        <p><img src="http://localhost/contents/uploads/2017/11/1.jpg" width="215" height="1515"></p>
        <p><img width="215" height="1515" src="http://localhost/contents/uploads/2017/11/1.png"></p>
          <p   ><img
            class="blah"
                height="1515" 
                width="215"
           src="http://localhost/contents/uploads/2017/11/1.png"
    
           >
        </p>
      </body>
    </html>
    HTML;
    
    $dom = new DOMDocument;
    libxml_use_internal_errors(true);
    $dom->loadHTML($html);
    libxml_use_internal_errors(false);
    
    $xpath = new DOMXPath($dom);
    foreach ($xpath->evaluate('//p[img]') as $paragraphWithImage) {
        $paragraphWithImage->setAttribute('class', 'foo');
        foreach ($paragraphWithImage->getElementsByTagName('img') as $image) {
            $image->setAttribute('class', trim('bar ' . $image->getAttribute('class')));
            $image->setAttribute('data-src', $image->getAttribute('src'));
            $image->removeAttribute('src');
        }
    };
    
    echo $dom->saveHTML($dom->documentElement);
    

    输出:

    <html><body>
        <p class="foo"><img width="215" height="1515" class="bar" data-src="http://localhost/contents/uploads/2017/11/1.jpg"></p>
        <p class="foo"><img width="215" height="1515" class="bar" data-src="http://localhost/contents/uploads/2017/11/1.png"></p>
          <p class="foo"><img class="bar blah" height="1515" width="215" data-src="http://localhost/contents/uploads/2017/11/1.png"></p>
      </body></html>
    

    【讨论】:

    • 不,它不是一个精确的 HTML,因为我想使用 preg_replace,使用 preg_replace 无法完成我需要的操作?
    • 为任意 html 编写可靠的模式是可能的,但非常困难。 dom 解析器更简单
    • @Gordon 不幸的是,我试图反驳 OP 使用 DomDocument 一些问题,但没有结果:(
    • @Gordon 谢谢,但我认为代码工作有问题并为

      标签添加了一个类,是的,但它没有改变图像的任何内容它没有为 添加类标记而不是将 src= 更改为 data-src=

    • @sadadadadasdad 抱歉。固定。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    • 2013-01-19
    相关资源
    最近更新 更多