The s9e\TextFormatter\Utils\ParsedDOM utility allows you to load the parsed XML representation of text (typically stored in a database) into a DOM document. You can then manipulate it using standard DOM methods and a specialized API.
Key Benefits:
- Provides better guarantees that the resulting XML will match what the parser produces compared to manual string manipulation.
- Ideal for maintenance tasks and complex transformations.
Note: For lightweight, real-time operations, use the more efficient s9e\TextFormatter\Utils class instead of ParsedDOM if possible.
// Start with the parsed representation of the text
$xml = '<r><p>Hello <EM><s>*</s>world<e>*</e></EM> 😀</p></r>';
// Load it into a DOM document
$dom = s9e\TextFormatter\Utils\ParsedDOM::loadXML($xml);
// Select each EM elements using XPath...
foreach ($dom->query('//EM') as $em)
{
// ...and unparse it
$em->unparse();
}
// Converting the document to a string will serialize it back to XML in a way that
// matches what the parser would output.
echo '__toString() ', (string) $dom . "\n";
echo 'saveXML() ', $dom->saveXML();