Skip to content
PHP Logo

How can PHP DOM functions handle escaping characters like <&>" automatically?

PHP DOM functions can automatically handle escaping characters like <&>" by using the `createTextNode` method to create text nodes within the DOM. This method will automatically escape special characters, ensuring that the content is rendered correctly without the need for manual escaping.

php
// Create a new DOMDocument
$doc = new DOMDocument();

// Create a text node with special characters
$text = $doc->createTextNode('<Hello & World>');

// Append the text node to the document
$doc->appendChild($text);

// Output the HTML
echo $doc->saveHTML();