PHP Conference Japan 2024

DOMComment 類別

(PHP 5, PHP 7, PHP 8)

簡介

表示註釋節點,由 <!----> 分隔的字元。

類別概要

類別 DOMComment 繼承自 DOMCharacterData {
/* 繼承的常數 */
/* 繼承的屬性 */
公開 字串 $data;
公開 唯讀 整數 $length;
公開 唯讀 字串 $nodeName;
公開 唯讀 整數 $nodeType;
公開 唯讀 ?DOMNode $parentNode;
公開 唯讀 ?DOMElement $parentElement;
公開 唯讀 DOMNodeList $childNodes;
公開 唯讀 ?DOMNode $firstChild;
公開唯讀 DOMNode $lastChild;
公開唯讀 DOMNode $previousSibling;
公開唯讀 DOMNode $nextSibling;
公開唯讀 DOMNamedNodeMap $attributes;
公開唯讀 bool $isConnected;
公開唯讀 DOMDocument $ownerDocument;
公開唯讀 ?string $namespaceURI;
公開 string $prefix;
公開唯讀 ?string $localName;
公開唯讀 ?string $baseURI;
公開 string $textContent;
/* 方法 */
公開 __construct(string $data = "")
/* 繼承方法 */
公開 DOMCharacterData::after(DOMNode|string ...$nodes): void
公開 DOMCharacterData::appendData(string $data): true
公開 DOMCharacterData::replaceData(整數 $offset, 整數 $count, 字串 $data): 布林
公開 DOMNode::C14N(
    布林 $exclusive = false,
    布林 $withComments = false,
    ?陣列 $xpath = null,
    ?陣列 $nsPrefixes = null
): 字串|false
public DOMNode::C14NFile(
    字串 $uri,
    布林 $exclusive = false,
    布林 $withComments = false,
    ?陣列 $xpath = null,
    ?陣列 $nsPrefixes = null
): 整數|false
公開 DOMNode::isSupported(字串 $feature, 字串 $version): 布林值
}

另請參閱

目錄

新增註釋

使用者貢獻的註釋 1 則註釋

npelov at croler dot net
3 年前
請注意,註釋的 textContent 屬性並未進行實體解碼,而是在 DOMElement::textContent 中解碼。

<?php

$xml
= new DOMDocument();
$xml->loadXML('<?xml version="1.0" encoding="utf-8"?>
<configuration version="2">
<!-- test &#45;&#45;&gt; -->
test &#45;&#45;&gt;
</configuration>'
);
$xpath = new DOMXPath($xml);
$comments = $xpath->query('//comment()');
$elements = $xpath->query('//configuration');

echo
$comments[0]->textContent;
// 結果: test &#45;&#45;&gt;

echo $elements[0]->textContent;
// 結果: test -->

?>
To Top