dom - php DomDocument adds extra tags -


i'm trying parse document , image tags , change source different.

      $domdocument = new domdocument();      $domdocument->loadhtml($text);      $imagenodelist = $domdocument->getelementsbytagname('img');      foreach ($imagenodelist $image) {       $image->setattribute('src', 'lalala');       $domdocument->savehtml($image);     }      $text = $domdocument->savehtml();  

the $text looks this:

 

<p>hi, test, here image<img src="http://mysite.com/beer.jpg" width="60" height="95" /> because beer!</p>

and output $text:

 

<!doctype html public "-//w3c//dtd html 4.0 transitional//en" "http://www.w3.org/tr/rec-html40/loose.dtd">
<html><body><p>hi, test, here image<img src="lalala" width="68" height="95"> because beer!</p></body></html>

i'm getting bunch of tags (html, body, , comment @ top) don't need. way set domdocument avoid adding these tags?

thank you!

domdocument unfortunately retarded , won't let this. try this:

$text = preg_replace('/^<!doctype.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $domdocument->savehtml())); 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

sql server - python to mssql encoding problem -

windows - Python Service Installation - "Could not find PythonClass entry" -