Post by Kentoranything else =/
I think you should go with the XMLWriter
However
I guess, if you're just putting some character data in semi-fixed tags
(rather than trying to generate the document structure from the db) you
could maybe escape the character data with something like this:
$original = "this&that ]]><foo";
$pattern = array("&", "<", "]]>");
$replacement = array("&", "<", "]]>");
$escaped = preg_replace($pattern, $replacement, $original);
I didn't test this, and whether this works is up to the implementation
of preg_replace()...
Note if you're planning to put strings into attribute values the ' and "
characters should be escaped as well. (With ' and " respectively.)
There is also a caveat; the XML 1.0 spec doesn't allow for some
"special" characters and therefore it is possible that the document is
still not well-formed even if the code does work as I intended. If
you're afraid of encountering characters not allowed to occur in XML 1.0
you should declare the XML version to be 1.1, which allows for almost
all unicode characters. That means you put the string
<?xml version="1.1"?>
first in the document.
--
- Tuomas