PHP 8.5.0 Beta 3 available for testing

DOMNode ใ‚ฏใƒฉใ‚น

(PHP 5, PHP 7, PHP 8)

ใ‚ฏใƒฉใ‚นๆฆ‚่ฆ

class DOMNode {
/* ๅฎšๆ•ฐ */
public const int DOCUMENT_POSITION_DISCONNECTED = 0x1;
public const int DOCUMENT_POSITION_PRECEDING = 0x2;
public const int DOCUMENT_POSITION_FOLLOWING = 0x4;
public const int DOCUMENT_POSITION_CONTAINS = 0x8;
public const int DOCUMENT_POSITION_CONTAINED_BY = 0x10;
/* ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃ */
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 C14N(
    bool $exclusive = false,
    bool $withComments = false,
    ?array $xpath = null,
    ?array $nsPrefixes = null
): string|false
public C14NFile(
    string $uri,
    bool $exclusive = false,
    bool $withComments = false,
    ?array $xpath = null,
    ?array $nsPrefixes = null
): int|false
public cloneNode(bool $deep = false): DOMNode|false
public getLineNo(): int
public getRootNode(?array $options = null): DOMNode
public insertBefore(DOMNode $node, ?DOMNode $child = null): DOMNode|false
public isDefaultNamespace(string $namespace): bool
public isEqualNode(?DOMNode $otherNode): bool
public isSameNode(DOMNode $otherNode): bool
public isSupported(string $feature, string $version): bool
public lookupPrefix(string $namespace): ?string
public normalize(): void
public replaceChild(DOMNode $node, DOMNode $child): DOMNode|false
public __sleep(): array
public __wakeup(): void
}

ๅฎš็พฉๆธˆใฟๅฎšๆ•ฐ

DOMNode::DOCUMENT_POSITION_DISCONNECTED
ใ‚‚ใ†ไธ€ๆ–นใฎใƒŽใƒผใƒ‰ใจๅ‚็…งใƒŽใƒผใƒ‰ใŒๅŒใ˜ใƒ„ใƒชใƒผๅ†…ใซใชใ„ๅ ดๅˆใซ่จญๅฎšใ•ใ‚Œใพใ™ใ€‚
DOMNode::DOCUMENT_POSITION_PRECEDING
ใ‚‚ใ†ไธ€ๆ–นใฎใƒŽใƒผใƒ‰ใŒๅ‚็…งใƒŽใƒผใƒ‰ใ‚ˆใ‚Šๅ‰ใซใ‚ใ‚‹ๅ ดๅˆใซ่จญๅฎšใ•ใ‚Œใพใ™ใ€‚
DOMNode::DOCUMENT_POSITION_FOLLOWING
ใ‚‚ใ†ไธ€ๆ–นใฎใƒŽใƒผใƒ‰ใŒๅ‚็…งใƒŽใƒผใƒ‰ใฎๅพŒใซ็ถšใๅ ดๅˆใซ่จญๅฎšใ•ใ‚Œใพใ™ใ€‚
DOMNode::DOCUMENT_POSITION_CONTAINS
ใ‚‚ใ†ไธ€ๆ–นใฎใƒŽใƒผใƒ‰ใŒๅ‚็…งใƒŽใƒผใƒ‰ใฎ็ฅ–ๅ…ˆใงใ‚ใ‚‹ๅ ดๅˆใซ่จญๅฎšใ•ใ‚Œใพใ™ใ€‚
DOMNode::DOCUMENT_POSITION_CONTAINED_BY
ใ‚‚ใ†ไธ€ๆ–นใฎใƒŽใƒผใƒ‰ใŒๅ‚็…งใƒŽใƒผใƒ‰ใฎๅญๅญซใงใ‚ใ‚‹ๅ ดๅˆใซ่จญๅฎšใ•ใ‚Œใพใ™ใ€‚
DOMNode::DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC
็ตๆžœใŒๅฎŸ่ฃ…ไพๅญ˜ใฎๅ‹•ไฝœใซๅŸบใฅใ„ใฆใŠใ‚Šใ€็งปๆคๆ€งใŒใชใ„ๅ ดๅˆใซ่จญๅฎšใ•ใ‚Œใพใ™ใ€‚ ใ“ใ‚Œใฏใ€ๅŒใ˜ใƒ„ใƒชใƒผๅ†…ใซใชใ„ใƒŽใƒผใƒ‰ใ‚„ๅฑžๆ€งใƒŽใƒผใƒ‰ใฎๅ ดๅˆใซ็™บ็”Ÿใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚

ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃ

nodeName

็พๅœจใฎใƒŽใƒผใƒ‰ๅž‹ใฎๆญฃ็ขบใชๅๅ‰ใ‚’่ฟ”ใ™

nodeValue

ใใฎๅž‹ใซๅฟœใ˜ใฆใƒŽใƒผใƒ‰ใฎๅ€คใ‚’่ฟ”ใ™ใ€‚ W3C ใฎไป•ๆง˜ใจใฏ็•ฐใชใ‚Šใ€ DOMElement ใƒŽใƒผใƒ‰ใฎๅ€คใฏ null ใงใฏใชใ DOMNode::textContent ใจ็ญ‰ใ—ใใชใ‚‹ใ€‚

nodeType

ใƒŽใƒผใƒ‰ใฎๅž‹ใ‚’ใ€ๅฎš็พฉๆธˆใฟใฎๅฎšๆ•ฐ XML_*_NODE ใฎใ„ใšใ‚Œใ‹ใง่ฟ”ใ™

parentNode

ใ“ใฎใƒŽใƒผใƒ‰ใฎ่ฆช (ๅญ˜ๅœจใ—ใชใ„ๅ ดๅˆใฏ null) ใ‚’่ฟ”ใ™

parentElement

ใ“ใฎ่ฆ็ด ใฎ่ฆช่ฆ็ด ใ€‚ๅญ˜ๅœจใ—ใชใ„ๅ ดๅˆใฏ null ใ‚’่ฟ”ใ™

childNodes

ใ“ใฎใƒŽใƒผใƒ‰ใฎใ™ในใฆใฎๅญใ‚’ๅซใ‚€ DOMNodeListใ€‚ ๅญใŒๅญ˜ๅœจใ—ใชใ„ๅ ดๅˆใฏใ€็ฉบใฎ DOMNodeList

firstChild

ใ“ใฎใƒŽใƒผใƒ‰ใฎๆœ€ๅˆใฎๅญใ€‚ๅญ˜ๅœจใ—ใชใ„ๅ ดๅˆใฏ null ใ‚’่ฟ”ใ™

lastChild

ใ“ใฎใƒŽใƒผใƒ‰ใฎๆœ€ๅพŒใฎๅญใ€‚ๅญ˜ๅœจใ—ใชใ„ๅ ดๅˆใฏ null ใ‚’่ฟ”ใ™

previousSibling

ใ“ใฎใƒŽใƒผใƒ‰ใฎ็›ดๅ‰ใฎใƒŽใƒผใƒ‰ใ€‚ๅญ˜ๅœจใ—ใชใ„ๅ ดๅˆใฏ null ใ‚’่ฟ”ใ™

nextSibling

ใ“ใฎใƒŽใƒผใƒ‰ใฎ็›ดๅพŒใฎใƒŽใƒผใƒ‰ใ€‚ๅญ˜ๅœจใ—ใชใ„ๅ ดๅˆใฏ null ใ‚’่ฟ”ใ™

attributes

ใ“ใฎใƒŽใƒผใƒ‰ใŒ DOMElement ใฎๅ ดๅˆใฏ ใƒŽใƒผใƒ‰ใฎๅฑžๆ€งใ‚’ๅซใ‚€ DOMNamedNodeMapใ€ ใใ‚Œไปฅๅค–ใฎๅ ดๅˆใฏ null

isConnected

ใƒŽใƒผใƒ‰ใŒ Document ใซๆŽฅ็ถšใ•ใ‚Œใฆใ„ใ‚‹ใ‹ใฉใ†ใ‹

ownerDocument

ใ“ใฎใƒŽใƒผใƒ‰ใซ้–ข้€ฃไป˜ใ‘ใ‚‰ใ‚Œใฆใ„ใ‚‹ DOMDocument ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ€‚ ใ“ใฎใƒŽใƒผใƒ‰ใŒ้–ข้€ฃไป˜ใ‘ใ‚‰ใ‚ŒใŸๆ–‡ๆ›ธใ‚’ๆŒใŸใชใ„ๅ ดๅˆใฏ null (ไพ‹: ใƒŽใƒผใƒ‰ใŒใƒ‡ใ‚ฟใƒƒใƒใ•ใ‚Œใฆใ„ใ‚‹ใ‹ใ€DOMDocument ใ ใฃใŸๅ ดๅˆ)

namespaceURI

ใ“ใฎใƒŽใƒผใƒ‰ใฎๅๅ‰็ฉบ้–“ URIใ€‚ๆŒ‡ๅฎšใ•ใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏ null

prefix

ใ“ใฎใƒŽใƒผใƒ‰ใฎๅๅ‰็ฉบ้–“ใƒ—ใƒฌใƒ•ใ‚ฃใƒƒใ‚ฏใ‚น

localName

ใ“ใฎใƒŽใƒผใƒ‰ใฎๅๅ‰ใฎใƒญใƒผใ‚ซใƒซ้ƒจๅˆ†ใ‚’่ฟ”ใ™

baseURI

ใ“ใฎใƒŽใƒผใƒ‰ใฎๅฎŒๅ…จใชใƒ™ใƒผใ‚น URIใ€‚ใ‚‚ใ—ๅฎŸ่ฃ…ใŒๅฎŒๅ…จใช URL ใ‚’ ใงใใชใ‹ใฃใŸๅ ดๅˆใฏ null

textContent

ใ“ใฎใƒŽใƒผใƒ‰ใจใใฎๅญๅญซใƒŽใƒผใƒ‰ใฎใƒ†ใ‚ญใ‚นใƒˆ

ๅค‰ๆ›ดๅฑฅๆญด

ใƒใƒผใ‚ธใƒงใƒณ ่ชฌๆ˜Ž
8.4.0 DOMNode::compareDocumentPosition() ใƒกใ‚ฝใƒƒใƒ‰ใŒ่ฟฝๅŠ ใ•ใ‚Œใพใ—ใŸใ€‚
8.4.0 ไปฅไธ‹ใฎๅฎšๆ•ฐใŒๆ–ฐใŸใซ่ฟฝๅŠ ใ•ใ‚Œใพใ—ใŸใ€‚ DOMNode::DOCUMENT_POSITION_DISCONNECTEDใ€ DOMNode::DOCUMENT_POSITION_PRECEDINGใ€ DOMNode::DOCUMENT_POSITION_FOLLOWINGใ€ DOMNode::DOCUMENT_POSITION_CONTAINSใ€ DOMNode::DOCUMENT_POSITION_CONTAINED_BYใ€ใŠใ‚ˆใณ DOMNode::DOCUMENT_POSITION_IMPLEMENTATION_SPECIFICใ€‚
8.3.0 DOMNode::contains() ใจใ€ DOMNode::isEqualNode() ใจใ„ใ†ใƒกใ‚ฝใƒƒใƒ‰ใŒ่ฟฝๅŠ ใ•ใ‚Œใพใ—ใŸใ€‚
8.3.0 ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃ DOMNode::$parentElement ใจ DOMNode::$isConnected ใŒ่ฟฝๅŠ ใ•ใ‚Œใพใ—ใŸใ€‚
8.0.0 ๅฎŸ่ฃ…ใ•ใ‚Œใฆใ„ใชใ‹ใฃใŸใƒกใ‚ฝใƒƒใƒ‰ DOMNode::compareDocumentPosition(), DOMNode::isEqualNode(), DOMNode::getFeature(), DOMNode::setUserData(), DOMNode::getUserData() ใŒๅ‰Š้™คใ•ใ‚Œใพใ—ใŸใ€‚

ๆณจๆ„

ๆณจๆ„:

DOMๆ‹กๅผตใƒขใ‚ธใƒฅใƒผใƒซ ใฏ UTF-8 ใ‚จใƒณใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใ‚’ไฝฟใ„ใพใ™ใ€‚ไป–ใฎใ‚จใƒณใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใ‚’ๆ‰ฑใ†ๅ ดๅˆใฏใ€mb_convert_encoding(), UConverter::transcode(), iconv() ใ‚’ไฝฟใฃใฆใใ ใ•ใ„ใ€‚

็›ฎๆฌก

  • DOMNode::appendChild โ€” ๅญ่ฆ็ด ็พคใฎๆœ€ๅพŒใซๆ–ฐใ—ใ„ๅญ่ฆ็ด ใ‚’่ฟฝๅŠ ใ™ใ‚‹
  • DOMNode::C14N โ€” ใƒŽใƒผใƒ‰ใ‚’ๆ–‡ๅญ—ๅˆ—ใซๆญฃ่ฆๅŒ–ใ™ใ‚‹
  • DOMNode::C14NFile โ€” ใƒŽใƒผใƒ‰ใ‚’ใƒ•ใ‚กใ‚คใƒซใซๆญฃ่ฆๅŒ–ใ™ใ‚‹
  • DOMNode::cloneNode โ€” ใƒŽใƒผใƒ‰ใ‚’่ค‡่ฃฝใ™ใ‚‹
  • DOMNode::compareDocumentPosition โ€” 2ใคใฎใƒŽใƒผใƒ‰ใฎไฝ็ฝฎใ‚’ๆฏ”่ผƒใ—ใพใ™ใ€‚
  • DOMNode::contains โ€” ใƒŽใƒผใƒ‰ใŒๅˆฅใฎใƒŽใƒผใƒ‰ใ‚’ๅซใ‚“ใงใ„ใ‚‹ใ‹ใ‚’่ชฟในใ‚‹
  • DOMNode::getLineNo โ€” ใƒŽใƒผใƒ‰ใŒๅญ˜ๅœจใ™ใ‚‹่กŒใฎ็•ชๅทใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚
  • DOMNode::getNodePath โ€” ใƒŽใƒผใƒ‰ใฎ XPath ใ‚’ๅ–ๅพ—ใ™ใ‚‹
  • DOMNode::getRootNode โ€” ใƒซใƒผใƒˆ(ๆ น)ใƒŽใƒผใƒ‰ใ‚’ๅ–ๅพ—ใ™ใ‚‹
  • DOMNode::hasAttributes โ€” ใƒŽใƒผใƒ‰ใŒๅฑžๆ€งใ‚’ไฟๆŒใ—ใฆใ„ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’่ชฟในใ‚‹
  • DOMNode::hasChildNodes โ€” ใƒŽใƒผใƒ‰ใŒๅญใ‚’ไฟๆŒใ—ใฆใ„ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’่ชฟในใ‚‹
  • DOMNode::insertBefore โ€” ๅ‚็…งใ—ใฆใ„ใ‚‹ใƒŽใƒผใƒ‰ใฎๅ‰ใซๆ–ฐใ—ใ„ๅญใ‚’่ฟฝๅŠ ใ™ใ‚‹
  • DOMNode::isDefaultNamespace โ€” ๆŒ‡ๅฎšใ—ใŸ namespaceURI ใŒใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎๅๅ‰็ฉบ้–“ใ‹ใฉใ†ใ‹ใ‚’่ชฟในใ‚‹
  • DOMNode::isEqualNode โ€” ใƒŽใƒผใƒ‰ใŒ็ญ‰ใ—ใ„ใ‹ใ‚’่ชฟในใ‚‹
  • DOMNode::isSameNode โ€” 2 ใคใฎใƒŽใƒผใƒ‰ใŒ็ญ‰ใ—ใ„ใ‹ใฉใ†ใ‹ใ‚’่ชฟในใ‚‹
  • DOMNode::isSupported โ€” ๆŒ‡ๅฎšใ—ใŸใƒใƒผใ‚ธใƒงใƒณใงๆฉŸ่ƒฝใŒใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’่ชฟในใ‚‹
  • DOMNode::lookupNamespaceURI โ€” ใƒ—ใƒฌใƒ•ใ‚ฃใƒƒใ‚ฏใ‚นใซๅŸบใฅใ„ใฆใ€ใƒŽใƒผใƒ‰ใฎๅๅ‰็ฉบ้–“ URI ใ‚’ๅ–ๅพ—ใ™ใ‚‹
  • DOMNode::lookupPrefix โ€” ๅๅ‰็ฉบ้–“ URI ใซๅŸบใฅใ„ใฆใ€ใƒŽใƒผใƒ‰ใฎๅๅ‰็ฉบ้–“ใƒ—ใƒฌใƒ•ใ‚ฃใƒƒใ‚ฏใ‚นใ‚’ๅ–ๅพ—ใ™ใ‚‹
  • DOMNode::normalize โ€” ใƒŽใƒผใƒ‰ใ‚’ๆญฃ่ฆๅŒ–ใ™ใ‚‹
  • DOMNode::removeChild โ€” ๅญ่ฆ็ด ็พคใ‹ใ‚‰ๅญ่ฆ็ด ใ‚’ๅ‰Š้™คใ™ใ‚‹
  • DOMNode::replaceChild โ€” ๅญใ‚’็ฝฎใๆ›ใˆใ‚‹
  • DOMNode::__sleep โ€” Forbids serialization unless serialization methods are implemented in a subclass
  • DOMNode::__wakeup โ€” Forbids unserialization unless unserialization methods are implemented in a subclass
๏ผ‹add a note

User Contributed Notes 13 notes

up
43
marc at ermshaus dot org ยถ
16 years ago
It took me forever to find a mapping for the XML_*_NODE constants. So I thought, it'd be handy to paste it here:

1 XML_ELEMENT_NODE
2 XML_ATTRIBUTE_NODE
3 XML_TEXT_NODE
4 XML_CDATA_SECTION_NODE
5 XML_ENTITY_REFERENCE_NODE
6 XML_ENTITY_NODE
7 XML_PROCESSING_INSTRUCTION_NODE
8 XML_COMMENT_NODE
9 XML_DOCUMENT_NODE
10 XML_DOCUMENT_TYPE_NODE
11 XML_DOCUMENT_FRAGMENT_NODE
12 XML_NOTATION_NODE
up
23
David Rekowski ยถ
15 years ago
You cannot simply overwrite $textContent, to replace the text content of a DOMNode, as the missing readonly flag suggests. Instead you have to do something like this:

<?php

$node
->removeChild($node->firstChild);
$node->appendChild(new DOMText('new text content'));

?>

This example shows what happens:

<?php

$doc
= DOMDocument::loadXML('<node>old content</node>');
$node = $doc->getElementsByTagName('node')->item(0);
echo
"Content 1: ".$node->textContent."\n";

$node->textContent = 'new content';
echo
"Content 2: ".$node->textContent."\n";

$newText = new DOMText('new content');

$node->appendChild($newText);
echo
"Content 3: ".$node->textContent."\n";

$node->removeChild($node->firstChild);
$node->appendChild($newText);
echo
"Content 4: ".$node->textContent."\n";

?>

The output is:

Content 1: old content // starting content
Content 2: old content // trying to replace overwriting $node->textContent
Content 3: old contentnew content // simply appending the new text node
Content 4: new content // removing firstchild before appending the new text node

If you want to have a CDATA section, use this:

<?php
$doc
= DOMDocument::loadXML('<node>old content</node>');
$node = $doc->getElementsByTagName('node')->item(0);
$node->removeChild($node->firstChild);
$newText = $doc->createCDATASection('new cdata content');
$node->appendChild($newText);
echo
"Content withCDATA: ".$doc->saveXML($node)."\n";
?>
up
20
R. Studer ยถ
15 years ago
For clarification:
The assumingly 'discoverd' by previous posters and seemingly undocumented methods (.getElementsByTagName and .getAttribute) on this class (DOMNode) are in fact methods of the class DOMElement, which inherits from DOMNode.

See: http://www.php.net/manual/en/class.domelement.php
up
10
brian wildwoodassociates.info ยถ
16 years ago
This class has a getAttribute method.

Assume that a DOMNode object $ref contained an anchor taken out of a DOMNode List. Then

$url = $ref->getAttribute('href');

would isolate the url associated with the href part of the anchor.
up
6
alastair dot dallas at gmail dot com ยถ
13 years ago
The issues around mixed content took me some experimentation to remember, so I thought I'd add this note to save others time.

When your markup is something like: <div><p>First text.</p><ul><li><p>First bullet</p></li></ul></div>, you'll get XML_ELEMENT_NODEs that are quite regular. The <div> has children <p> and <ul> and the nodeValue for both <p>s yields the text you expect.

But when your markup is more like <p>This is <b>bold</b> and this is <i>italic</i>.</p>, you realize that the nodeValue for XML_ELEMENT_NODEs is not reliable. In this case, you need to look at the <p>'s child nodes. For this example, the <p> has children: #text, <b>, #text, <i>, #text.

In this example, the nodeValue of <b> and <i> is the same as their #text children. But you could have markup like: <p>This <b>is bold and <i>bold italic</i></b>, you see?</p>. In this case, you need to look at the children of <b>, which will be #text, <i>, because the nodeValue of <b> will not be sufficient.

XML_TEXT_NODEs have no children and are always named '#text'. Depending on how whitespace is handled, your tree may have "empty" #text nodes as children of <body> and elsewhere.

Attributes are nodes, but I had forgotten that they are not in the tree expressed by childNodes. Walking the full tree using childNodes will not visit any attribute nodes.
up
4
pizarropablo at gmail dot com ยถ
11 years ago
In response to: alastair dot dallas at gmail dot com about "#text" nodes.
"#text" nodes appear when there are spaces or new lines between end tag and next initial tag.

Eg "<data><age>10</age>[SPACES]<other>20</other>[SPACES]</data>"

"data" childNodes has 4 childs:
- age = 10
- #text = spaces
- other = 20
- #text = spaces
up
8
imranomar at gmail dot com ยถ
14 years ago
Just discovered that node->nodeValue strips out all the tags
up
5
Steve K ยถ
15 years ago
This class apparently also has a getElementsByTagName method.

I was able to confirm this by evaluating the output from DOMNodeList->item() against various tests with the is_a() function.
up
3
metanull ยถ
11 years ago
Yet another DOMNode to php array conversion function.
Other ones on this page are generating too "complex" arrays; this one should keep the array as tidy as possible.
Note: make sure to set LIBXML_NOBLANKS when calling DOMDocument::load, loadXML or loadHTML
See: http://be2.php.net/manual/en/libxml.constants.php
See: http://be2.php.net/manual/en/domdocument.loadxml.php

<?php
/**
* Returns an array representation of a DOMNode
* Note, make sure to use the LIBXML_NOBLANKS flag when loading XML into the DOMDocument
* @param DOMDocument $dom
* @param DOMNode $node
* @return array
*/
function nodeToArray( $dom, $node) {
if(!
is_a( $dom, 'DOMDocument' ) || !is_a( $node, 'DOMNode' )) {
return
false;
}
$array = false;
if( empty(
trim( $node->localName ))) {// Discard empty nodes
return false;
}
if(
XML_TEXT_NODE == $node->nodeType ) {
return
$node->nodeValue;
}
foreach (
$node->attributes as $attr) {
$array['@'.$attr->localName] = $attr->nodeValue;
}
foreach (
$node->childNodes as $childNode) {
if (
1 == $childNode->childNodes->length && XML_TEXT_NODE == $childNode->firstChild->nodeType ) {
$array[$childNode->localName] = $childNode->nodeValue;
} else {
if(
false !== ($a = self::nodeToArray( $dom, $childNode))) {
$array[$childNode->localName] = $a;
}
}
}
return
$array;
}
?>
up
2
matej dot golian at gmail dot com ยถ
12 years ago
Here is a little function that truncates a DomNode to a specified number of text characters. I use it to generate HTML excerpts for my blog entries.

<?php

function makehtmlexcerpt(DomNode $html, $excerptlength)
{
$remove = 0;
$htmllength = strlen(html_entity_decode($html->textContent, ENT_QUOTES, 'UTF-8'));
$truncate = $htmllength - $excerptlength;
if(
$htmllength > $excerptlength)
{
if(
$html->hasChildNodes())
{
$children = $html->childNodes;
for(
$counter = 0; $counter < $children->length; $counter ++)
{
$child = $children->item($children->length - ($counter + 1));
$childlength = strlen(html_entity_decode($child->textContent, ENT_QUOTES, 'UTF-8'));
if(
$childlength <= $truncate)
{
$remove ++;
$truncate = $truncate - $childlength;
}
else
{
$child = makehtmlexcerpt($child, $childlength - $truncate);
break;
}
}
if(
$remove != 0)
{
for(
$counter = 0; $counter < $remove; $counter ++)
{
$html->removeChild($html->lastChild);
}
}
}
else
{
if(
$html->nodeName == '#text')
{
$html->nodeValue = substr(html_entity_decode($html->nodeValue, ENT_QUOTES, 'UTF-8'), 0, $htmllength - $truncate);
}
}
}
return
$html;
}

?>
up
5
matt at lamplightdb dot co dot uk ยถ
16 years ago
And apparently also a setAttribute method too:

$node->setAttribute( 'attrName' , 'value' );
up
-1
Anonymous ยถ
7 years ago
It would be helpful if docs for concrete properties mentioned readonly status of some properties:
"
ownerDocument

The DOMDocument object associated with this node.

"
up
-1
zlk1214 at gmail dot com ยถ
9 years ago
A function that can set the inner HTML without encoding error. $html can be broken content such as "<a ID=id20>ssss"
function setInnerHTML($node, $html) {
removeChildren($node);
if (empty($html)) {
return;
}

$doc = $node->ownerDocument;
$htmlclip = new DOMDocument();
$htmlclip->loadHTML('<meta http-equiv="Content-Type" content="text/html;charset=utf-8"><div>' . $html . '</div>');
$clipNode = $doc->importNode($htmlclip->documentElement->lastChild->firstChild, true);
while ($item = $clipNode->firstChild) {
$node->appendChild($item);
}
}
To Top