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 );
}
}
}
}The DOMText class
(PHP 5, PHP 7, PHP 8)
Introduction
The DOMText class inherits from DOMCharacterData and represents the textual content of a DOMElement or DOMAttr.
Class synopsis
/* Inherited constants */
/* Properties */
/* Inherited properties */
/* Methods */
/* Inherited methods */
public function DOMNode::C14N(
bool
bool
?array
?array
): string|false
}bool
$exclusive = false,bool
$withComments = false,?array
$xpath = null,?array
$nsPrefixes = null): string|false
Properties
- wholeText
-
Holds all the text of logically-adjacent (not separated by Element, Comment or Processing Instruction) Text nodes.
Changelog
| Version | Description |
|---|---|
| 8.0.0 | The unimplemented method DOMText::replaceWholeText() has been removed. |
Table of Contents
- DOMText::__construct — Creates a new DOMText object
- DOMText::isElementContentWhitespace — Returns whether this text node contains whitespace in element content
- DOMText::isWhitespaceInElementContent — Indicates whether this text node contains whitespace
- DOMText::splitText — Breaks this node into two nodes at the specified offset
+add a note
User Contributed Notes 1 note
Trititaty ¶
9 years ago