(PHP 5, PHP 7, PHP 8)
The DOMText class inherits from DOMCharacterData and represents the textual content of a DOMElement or DOMAttr .
$exclusive
= false
,$withComments
= false
,$xpath
= null
,$nsPrefixes
= null
Holds all the text of logically-adjacent (not separated by Element, Comment or Processing Instruction) Text nodes.
Version | Description |
---|---|
8.0.0 | The unimplemented method DOMText::replaceWholeText() has been removed. |
Text replacement function for DOM.
<?php
function domTextReplace( $search, $replace, DOMNode &$domNode, $isRegEx = false ) {
if ( $domNode->hasChildNodes() ) {
$children = array();
// since looping through a DOM being modified is a bad idea we prepare an array:
foreach ( $domNode->childNodes as $child ) {
$children[] = $child;
}
foreach ( $children as $child ) {
if ( $child->nodeType === XML_TEXT_NODE ) {
$oldText = $child->wholeText;
if ( $isRegEx ) {
$newText = preg_replace( $search, $replace, $oldText );
} else {
$newText = str_replace( $search, $replace, $oldText );
}
$newTextNode = $domNode->ownerDocument->createTextNode( $newText );
$domNode->replaceChild( $newTextNode, $child );
} else {
domTextReplace( $search, $replace, $child, $isRegEx );
}
}
}
}