PHP str_replace() 和 preg_replace() 不适用于 HTML

PHP str_replace() and preg_replace() not working with HTML

当我尝试在函数内执行 str_replace()preg_replace() 时,内容没有改变。

变量$sadrzaj中的内容:

$sadrzaj = '<p>asdasdasds</p><p><a href="http://www.example.com/wp-content/uploads/2018/11/image.jpg" itemprop="url" title="some title"><img alt="some alt title" class="alignnone size-full wp-image-243618" src="http://www.example.com/wp-content/uploads/2018/11/image.jpg" width="940" height="529"></a></p>asdasdasd<p>asdasd</p><h3>asdada</h3><p><a href="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" itemprop="url" title="some title 02"><img alt="some alt title 02" class="alignnone size-full wp-image-243653" src="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" width="940" height="529"></a></p><h3>asdasd</h3>';

我的函数to_je_to():

function to_je_to($content){
    preg_match_all('/<img (.*?)\/>/', $content, $images);
    //print_r($images);

    if(!is_null($images)){
        foreach($images[1] as $index => $value){
            if(strpos($images[1], 'size-full') !== false){
            //if(preg_match('/alt=""/', $value)){
                $new_img = preg_replace('<img', "<img data-example", $images[0][$index]);
                $content = preg_replace($images[0][$index], $new_img, $content);
            }
        }
    }
    echo $content; // return no difference
}

调用函数 to_je_to($sadrzaj); - 没有任何变化。

如果有 class 和 "size-full",找到这张图片并将它们的标签替换为 <img data-example ...>

甚至 str_replace()preg_replace() 也不起作用。

我做错了什么?

谢谢

您的功能存在一些问题(在您的 preg_match_allpreg_replace 中)。此外,在匹配 <img> 标签上的 class 方面,您需要更加复杂。总的来说,您最好使用内置的 DOMDocument class 作为其他答案的建议。如果您想继续使用正则表达式,此功能应该可以解决您遇到的问题。

function to_je_to($content){
    preg_match_all('/<img[^>]+>/', $content, $images);

    if(!is_null($images)){
        foreach($images[0] as $index => $value){
            if(preg_match('/class="[^"]*(?<=["\s])size-full[\s"]/', $value)){
                $new_img = str_replace('<img', '<img data-example', $value);
                $content = preg_replace('/' . preg_quote($value, '/') . '/', $new_img, $content);
            }
        }
    }
    return $content; // return no difference
}
echo to_je_to($sadrzaj);

Demo on 3v4l.org

你做错的是用正则表达式解析 HTML。您应该使用适当的 DOM 解析器,然后您可以使用 XPath 查询来隔离所需的元素。

<?php
$sadrzaj = '<p>asdasdasds</p><p><a href="http://www.example.com/wp-content/uploads/2018/11/image.jpg" itemprop="url" title="some title"><img alt="some alt title" class="alignnone size-full wp-image-243618" src="http://www.example.com/wp-content/uploads/2018/11/image.jpg" width="940" height="529"></a></p>asdasdasd<p>asdasd</p><h3>asdada</h3><p><a href="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" itemprop="url" title="some title 02"><img alt="some alt title 02" class="alignnone size-full wp-image-243653" src="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" width="940" height="529"></a></p><h3>asdasd</h3>';

function to_je_to($content) {
    $dom = new DomDocument;
    $dom->loadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
    $xp = new DomXpath($dom);
    $nodes = $xp->query("//img[contains(concat(' ', normalize-space(@class), ' '), ' size-full ')]");
    foreach ($nodes as $img) {
        $img->setAttribute("data-example", "");
    }
    return $dom->saveHTML();
}
echo to_je_to($sadrzaj);

并且,评论您的原始代码:$images 永远不会是 null,它永远是一个数组。为什么循环 $images[1] 然后替换基于 $images[0] 的值?根本没有使用组匹配 (.*?),所以它不需要存在。循环中的两个 preg_replace() 调用都没有在要搜索的表达式周围使用定界符,因此两者都会因错误而失败。而且echoreturn的区别非常大。