note 80171 deleted from ref.simplexml by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 May 2009 01:16:49 +0000 |
| Subject: | note 80171 deleted from ref.simplexml by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155506@lists.php.net to get a copy of this message | ||
Note Submitter: tom at canobe dot com
----
The earlier poster is right in that simplexml becomes pretty useless when you have mixed-content
XML. This means that you basically cant use simplexml for (X)HTML, which is a bit of a blow.
Here's a potential solution (hack) for those of you just finding out the mixed-content problem
and not wanting to rewrite using DOM. This probably only really works if you have well-formed xhtml,
which in my case I have from running some html through Tidy beforehand.
Before passing to simplexml, run a couple of regexs on the html to put all the mixed content inside
<span>s. This gets handled as separate elements in the tree.
// put all text content inside a <span> because simplexmlelement
// doesnt handle mixed content.
// lesson learnt: dont use simplexmlelement for html
$html = preg_replace('|>\s*<|','><',$html);
$html =
preg_replace('|>\s*([^<]+)\s*|','><span>\1</span>',$html);