如何获取 HTML 文档中的所有 TEXT 外部元素
How to get all TEXT outside elements in a HTML document
我正在使用 Symfony DomCrawler 获取文档中的所有文本。
$this->crawler->filter('p')->each(function (Crawler $node, $i) {
// process text
});
我正在尝试收集 <body>
中元素之外的所有文本。
<body>
This is an example
<p>
blablabla
</p>
another example
<p>
<span>Yo!</span>
again, another piece of text <br/>
with an annoy BR in the middle
</p>
</body>
我正在使用 PHP Symfony,可以使用 XPath(首选)或 RegEx。
整个文档的字符串值可以用这个简单的XPath获取:
string(/)
文档中的所有文本节点将是:
//text()
body
的直接文本节点子节点为:
/body/text()
请注意,select 文本节点的 XPath 通常会根据上下文转换为连接的字符串值。
我正在使用 Symfony DomCrawler 获取文档中的所有文本。
$this->crawler->filter('p')->each(function (Crawler $node, $i) {
// process text
});
我正在尝试收集 <body>
中元素之外的所有文本。
<body>
This is an example
<p>
blablabla
</p>
another example
<p>
<span>Yo!</span>
again, another piece of text <br/>
with an annoy BR in the middle
</p>
</body>
我正在使用 PHP Symfony,可以使用 XPath(首选)或 RegEx。
整个文档的字符串值可以用这个简单的XPath获取:
string(/)
文档中的所有文本节点将是:
//text()
body
的直接文本节点子节点为:
/body/text()
请注意,select 文本节点的 XPath 通常会根据上下文转换为连接的字符串值。