如何从对象中删除xml标头标签后,与它的一些操作.PHP的DOM


How to remove xml header tags from object after some manipulation with it. PHP DOM

我有这样的代码

$doc = new DOMDocument();
$doc->loadHTML($article_header);
$imgs = $doc->getElementsByTagName('img');
foreach ($imgs as $img) {
     if(!$img->getAttribute('class')){
$src = $img->getAttribute('src');
$newSRC = str_replace('/img/', '/mini/', $src);
$img->setAttribute('src', $newSRC);
$article_header = $doc->saveXml();
            }
                    }

它可以很好地将$article_header与一些文本和图像一起使用,并在其中进行一些更改。问题是DOM将自己的代码添加到我的$article_header变量中。有一些XML头等…

也因为它是一个文章编辑器,我可以编辑我的文章10次,每次我运行这个脚本它添加一些xml语句,所以在几次运行后,我的$article_header在mysql数据库看起来像这样

<?xml version="1.0" standalone="yes"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<!--?xml version="1.0" standalone="yes"?-->
<html><body><p>&#xA0;</p>&#xD;
<p>&#xA0;</p>&#xD;
<!--?xml version="1.0" standalone="yes"?-->&#xD;
<p>&#xA0;</p>&#xD;
<p>&#xA0;</p>&#xD;
<!--?xml version="1.0" standalone="yes"?-->&#xD;
<p>1<img src="sources/public/users/103/articles/2011-06-11/3/img/1170x1600.gif" alt="" width="528" height="722.0512820512821"/>f</p>&#xD;
<!--?xml version="1.0" standalone="yes"?--></body></html>

问题是我如何摆脱所有这些额外的数据,这使我的问题,因为我的文章开始越来越低,因为这段代码是使多个<br>效果

您应该使用saveHTML()而不是saveXML()

details——http://php.net/manual/en/domdocument.savehtml.php