How to remove xml header tags from object after some manipulation with it. PHP DOM
I have this peace of code
$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();
}
}
It works fine taking $article_header with some text and images and making some changes inside of it. Problem is that DOM adds it's own code to my $article_header variable. There are some XML headers and etc...
Also because of the fact that it is an article editor and I can edit my article 10 times, each time I run this scrip开发者_运维知识库t it adds some xml statements so in the end after several runs my $article_header in mysql database looks like this
<?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> </p>
<p> </p>
<!--?xml version="1.0" standalone="yes"?-->
<p> </p>
<p> </p>
<!--?xml version="1.0" standalone="yes"?-->
<p>1<img src="sources/public/users/103/articles/2011-06-11/3/img/1170x1600.gif" alt="" width="528" height="722.0512820512821"/>f</p>
<!--?xml version="1.0" standalone="yes"?--></body></html>
Question is how can I get rid of all this additional data which is making me problems , because of this my article starts lower and lower becase this code is making multiple <br>
effect.
you should use saveHTML()
instead of saveXML()
details -- http://php.net/manual/en/domdocument.savehtml.php
精彩评论