note 96709 added to class.domdocument

From: Date: Fri, 12 Mar 2010 10:12:02 +0000
Subject: note 96709 added to class.domdocument
Groups: php.notes 
Request: Send a blank email to php-notes+get-166949@lists.php.net to get a copy of this message
After seeing many complaints about certain DOMDocument shortcomings, such as bad handling of encodings and always saving HTML fragments with <html>, <head>, and DOCTYPE, I decided that a better solution is needed. So here it is: SmartDOMDocument. You can find it at http://beerpla.net/projects/smartdomdocument/ Currently, the main highlights are: - SmartDOMDocument inherits from DOMDocument, so it's very easy to use - just declare an object of type SmartDOMDocument instead of DOMDocument and enjoy the new behavior on top of all existing functionality (see example below). - saveHTMLExact() - DOMDocument has an extremely badly designed "feature" where if the HTML code you are loading does not contain <html> and <body> tags, it adds them automatically (yup, there are no flags to turn this behavior off). Thus, when you call $doc->saveHTML(), your newly saved content now has <html><body> and DOCTYPE in it. Not very handy when trying to work with code fragments (XML has a similar problem). SmartDOMDocument contains a new function called saveHTMLExact() which does exactly what you would want - it saves HTML without adding that extra garbage that DOMDocument does. - encoding fix - DOMDocument notoriously doesn't handle encoding (at least UTF-8) correctly and garbles the output. SmartDOMDocument tries to work around this problem by enhancing loadHTML() to deal with encoding correctly. This behavior is transparent to you - just use loadHTML() as you would normally. - SmartDOMDocument Object As String - you can use a SmartDOMDocument object as a string which will print out its contents. For example: <?php echo "Here is the HTML: $smart_dom_doc"; ?> I'm going to maintain this code and try to fix bugs as they come in. Enjoy. ---- Server IP: 69.147.83.197 Probable Submitter: 76.126.68.79 ---- Manual Page -- http://www.php.net/manual/en/class.domdocument.php Edit -- https://master.php.net/note/edit/96709 Del: integrated -- https://master.php.net/note/delete/96709/integrated Del: useless -- https://master.php.net/note/delete/96709/useless Del: bad code -- https://master.php.net/note/delete/96709/bad+code Del: spam -- https://master.php.net/note/delete/96709/spam Del: non-english -- https://master.php.net/note/delete/96709/non-english Del: in docs -- https://master.php.net/note/delete/96709/in+docs Del: other reasons-- https://master.php.net/note/delete/96709 Reject -- https://master.php.net/note/reject/96709 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#166949) next »