Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: "Aaron Gray" Newsgroups: comp.lang.php Subject: DOMDocument HTML problem Date: Tue, 21 Feb 2012 19:00:51 -0000 Lines: 58 Message-ID: <9qi7vtFfmvU1@mid.individual.net> Mime-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit X-Trace: individual.net ZU6YleVZbHnktYeYQNknnwYHRcjKYydVbN9RSQmjCKg0eApwSA Cancel-Lock: sha1:TUPYV2Wgtv+d/ha43V9fZpOKGPM= X-Priority: 3 X-MSMail-Priority: Normal Importance: Normal X-Newsreader: Microsoft Windows Live Mail 15.4.3538.513 X-MimeOLE: Produced By Microsoft MimeOLE V15.4.3538.513 Xref: x330-a1.tempe.blueboxinc.net comp.lang.php:4921 Hi, I am trying to take an incomplete "summary" of a number of characters of an HTML fragment without DOCTYPE, HTML, or BODY elements and a possibly incomplete unbalanced fragment, and convert it to a ballanced fragment without DOCTYPE, HTML, or BODY, using DOMDocument and friends. This is what I have got (it works without a Error 500 using php command line tool :- ~~~~

foo



name
'; $dom = new DOMDocument(); $newdom = new DOMDocument(); $dom->loadHTML($html); $Elements = $dom->childNodes; foreach ( $Elements as $Element ) { $NewElement = $newdom->createElement($Element->nodeName); if ($Element->attributes) foreach($Element->attributes as $attribute) $NewElement->setAttribute($attribute->name, $attribute->value); if ($Element->childNodes) foreach($Element->childNodes as $child) $NewElement->appendChild( $newdom->importNode($child, true)); $newdom->appendChild( $NewElement); } echo $newdom->saveHTML(); ?> ~~~~ It balances the unbalanced
seems to be adding a duplicate at the beginning of the output. Also I need to get the fragment of the code like a JavaScript DOM innerHTML without the and tags. Many thanks in advance. Hope you can help, Aaron