PHP Conference Nagoya 2025

DOMText クラス

(PHP 5, PHP 7, PHP 8)

はじめに

DOMText クラスは DOMCharacterData を継承しており、 DOMElement あるいは DOMAttr の中身のテキストを表します。

クラス概要

class DOMText extends DOMCharacterData {
/* プロパティ */
public readonly string $wholeText;
/* 継承したプロパティ */
public string $data;
public readonly int $length;
public readonly string $nodeName;
public readonly int $nodeType;
public readonly ?DOMNode $parentNode;
public readonly ?DOMElement $parentElement;
public readonly DOMNodeList $childNodes;
public readonly ?DOMNode $firstChild;
public readonly ?DOMNode $lastChild;
public readonly ?DOMNode $previousSibling;
public readonly ?DOMNode $nextSibling;
public readonly ?DOMNamedNodeMap $attributes;
public readonly bool $isConnected;
public readonly ?DOMDocument $ownerDocument;
public readonly ?string $namespaceURI;
public string $prefix;
public readonly ?string $localName;
public readonly ?string $baseURI;
/* メソッド */
public __construct(string $data = "")
public splitText(int $offset): DOMText|false
/* 継承したメソッド */
public DOMCharacterData::deleteData(int $offset, int $count): bool
public DOMCharacterData::replaceData(int $offset, int $count, string $data): bool
public DOMNode::C14N(
    bool $exclusive = false,
    bool $withComments = false,
    ?array $xpath = null,
    ?array $nsPrefixes = null
): string|false
public DOMNode::C14NFile(
    string $uri,
    bool $exclusive = false,
    bool $withComments = false,
    ?array $xpath = null,
    ?array $nsPrefixes = null
): int|false
public DOMNode::isEqualNode(?DOMNode $otherNode): bool
public DOMNode::isSameNode(DOMNode $otherNode): bool
public DOMNode::isSupported(string $feature, string $version): bool
}

プロパティ

wholeText

論理的に隣接した (要素やコメント、処理命令などで分割されていない) テキストノードの全テキストを保持します。

変更履歴

バージョン 説明
8.0.0 実装されていなかったメソッド DOMText::replaceWholeText() が削除されました。

目次

add a note

User Contributed Notes 1 note

up
0
Trititaty
8 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