简体   繁体   English

如何在php中显示DOMElement类的对象

[英]how to display Object of class DOMElement in php

need your help.. I've seen a great answer from Dynamically replace the “src” attributes of all tags (redux) , but I don't know how to display from the code below 需要您的帮助。.我从动态替换所有标签(redux)的“ src”属性中看到了一个不错的答案,但是我不知道如何从下面的代码中显示

$dom=new DOMDocument();
$dom->loadHTML($your_html);
$imgs = $dom->getElementsByTagName("img");
foreach($imgs as $img){
    $alt = $img->getAttribute('alt');
    if ($alt == 'pumpkin'){
        $src = 'http://myhost.com/cache/img001.gif'; 
    } else if ($alt== '*'){
        $src = 'http://myhost.com/cache/img002.gif';
    } else if ($alt== 'cool image'){
        $src = 'http://myhost.com/cache/img003.jpg';
    }
    $img->setAttribute( 'src' , $src );
}

Use the DOMDocument::saveHTML() method, after you modify the image, by changing this line: 在修改图像之后,通过更改以下行,使用DOMDocument::saveHTML()方法:

$img->setAttribute( 'src' , $src );

To this: 对此:

$img->setAttribute( 'src' , $src );
echo $dom->saveHTML( $img);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM