【发布时间】:2010-11-30 14:09:06
【问题描述】:
我有以下代码来检索 HTML 文档中的所有超链接
我的问题是如何检索每个锚标记内的文本节点
(即使文本节点是子节点的子节点,例如锚节点有一个跨度节点,该节点有一个文本节点)?
<?PHP
$content = "
<html>
<head>
<title>bar , this is an example</title>
</head>
<body>
<a href='aaa'><span>bbb</span></a>
</body>
</html>
";
$dom = new DOMDocument();
@$dom->loadHTML($content);
$xpath = new DOMXPath($dom);
$row = $xpath->evaluate("/html/body//a");
for ($i = 0; $i < $row->length; $i++) {
$anchor = $row->item($i);
$href = $anchor->getAttribute('href');
// I want the grab the text value which is inside the anchor
$text = //should have the value "bbb"
}
?>
谢谢
【问题讨论】: