Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中显示DomeElement类的对象_Php - Fatal编程技术网

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

如何在php中显示DomeElement类的对象,php,Php,我需要你的帮助。。 我从中看到了一个很好的答案,但我不知道如何从下面的代码中显示 $dom=new DOMDocument(); $dom->loadHTML($your_html); $imgs = $dom->getElementsByTagName("img"); foreach($imgs as $img){ $alt = $img->getAttribute('alt'); if ($alt == 'pumpkin'){ $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 );
}
修改图像后,通过更改此行使用以下方法:

$img->setAttribute( 'src' , $src );
为此:

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