(PHP 5, PHP 7, PHP 8)
La clase DOMText hereda de DOMCharacterData y representa el contenido textual de DOMElement o DOMAttr .
$exclusive = false ,$withComments = false ,$xpath = null ,$nsPrefixes = null Contiene todo el texto de los nodos de texto adyacentes (es decir, nodos que no están separados por etiquetas Element, Comment o Processing Instruction).
| Versión | Descripción |
|---|---|
| 8.0.0 | El método no implementado DOMText::replaceWholeText() ha sido eliminado. |
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 );
}
}
}
}