La clase DOMText

(PHP 5, PHP 7, PHP 8)

Introducción

La clase DOMText hereda de DOMCharacterData y representa el contenido textual de un objeto DOMElement o DOMAttr.

Sinopsis de la Clase

class DOMText extends DOMCharacterData {
/* Propiedades */
readonly public string $wholeText;
/* Métodos */
public __construct(string $value = ?)
public splitText(int $offset): DOMText
/* Métodos heredados */
public DOMNode::C14N(
    bool $exclusive = ?,
    bool $with_comments = ?,
    array $xpath = ?,
    array $ns_prefixes = ?
): string
public DOMNode::C14NFile(
    string $uri,
    bool $exclusive = false,
    bool $with_comments = false,
    array $xpath = ?,
    array $ns_prefixes = ?
): int
public DOMNode::cloneNode(bool $deep = ?): DOMNode
public DOMNode::getLineNo(): int
public DOMNode::getNodePath(): string
public DOMNode::hasAttributes(): bool
public DOMNode::hasChildNodes(): bool
public DOMNode::insertBefore(DOMNode $newnode, DOMNode $refnode = ?): DOMNode
public DOMNode::isDefaultNamespace(string $namespaceURI): bool
public DOMNode::isSameNode(DOMNode $node): bool
public DOMNode::isSupported(string $feature, string $version): bool
public DOMNode::lookupNamespaceUri(string $prefix): string
public DOMNode::lookupPrefix(string $namespaceURI): string
public DOMNode::normalize(): void
public DOMNode::replaceChild(DOMNode $newnode, DOMNode $oldnode): DOMNode
}

Propiedades

wholeText

Mantiene todo el texto de los nodos Text adyacentes de forma lógica (sin estar separados por Element, Comment o Processing Instruction).

Tabla de contenidos

add a note add a note

User Contributed Notes 1 note

up
0
Trititaty
5 years ago
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 );
      }
    }
  }
}
To Top