我正在尝试将单个 html 段落解析为其构建块数组 - 我有这个 html 段落:
$element_content = '<p>Start of paragraph - <strong><em>This note</em></strong> provides <em>information</em> about the contractual terms.</p>';
到目前为止,我所做的是这样的:
$dom = new DOMDocument();
$dom->loadXML($element_content);
foreach ($dom->getElementsByTagName('*') as $node) {
echo $node->getNodePath().'<br>';
echo $node->nodeValue.'<br>';
}
这给了我这个结果:
/p
Start of paragraph - This note provides information about the contractual terms.
/p/strong
This note
/p/strong/em
This note
/p/em
information
但我想实现这一点:
/p
Start of paragraph -
/p/strong/em
This note
/p
provides
/p/em
information
/p
about the contractual terms.
关于如何实现它的任何想法?
DOM 中的所有内容都是一个节点。不仅是元素,文本也是如此。您正在获取元素节点,但您的结果会单独输出文本节点。因此,您需要获取不仅仅是空格节点的 DOM 文本节点。使用 Xpath 表达式并不难:
//text()[normalize-space(.) != ""]
//text()
提取文档中的任何文本节点(这包括 CDATA 部分(。 normalize-space()
是一个 Xpath 函数,可将字符串内的空格组减少为单个空格。前导和尾随空格将被删除。因此,[normalize-space(.) != ""]
从列表中删除仅包含空格的所有节点。
每个文本节点的父节点是其元素。拼:
$document = new DOMDocument();
$document->loadXML($content);
$xpath = new DOMXpath($document);
$nodes = $xpath->evaluate('//text()[normalize-space(.) != ""]');
foreach ($nodes as $node) {
echo $node->parentNode->getNodePath(), "n";
echo $node->textContent, "n";
}
输出:
/p
Start of paragraph -
/p/strong/em
This note
/p
provides
/p/em
information
/p
about the contractual terms.