【发布时间】:2012-02-27 12:00:19
【问题描述】:
我正在使用以下代码
$page_data=array();//$title_links is array having urls
$nodearr=array();
foreach ($title_links as $b_url)
{
$page_data[]= mycurl($b_url);//my curl function, it is okay
$dom2 = new DOMDocument();//creating new domdocument object inside a domdocument object
@$dom2->loadHTML($page_data[]);
$xpath_cat = new DOMXPath($dom2);//same as domdocument , nesting xpath
$nodelist = $xpath_cat->query('//span[@class="zg_hrsr_ladder"]', $dom2);
//echo nodeslist;
foreach ($nodelist as $node) {
$nodearr[] = $node->textContent;
}
}
所以场景是,
我将一个“domdocument”嵌套在另一个“DomDocument”中 使用“foreach 循环”,我在 DomDocument 中插入数组元素 另外,因为我是在 domdocument 中嵌套 domdocument,所以我是 声明新的 xpath 查询
问题是……
澄清:
$title_links 是使用 curl 获取的,是 urls 使用 domdocument 和 xpath 查询从网页中提取。 所以 $title_links 位于 xpath 和 domdocument 的第一级,$nodelist 处于嵌套 od xpath 和 dom-document 的第二层
【问题讨论】:
-
最终目标到底是什么?你是如何精确嵌套 DOMDocuments 的——你是在扩展那个类吗?这是你扩展的方法之一?
-
不,我没有在代码中使用 OOP 或类,它是简单的 php 代码,最后,我想在“mycurl( )”,无法运行查询。所以我决定在进一步进行之前明确这个概念
-
不幸的是,我对概念和场景都不清楚。为什么要使用 cURL 来获取 URL,而不是 php.net/manual/en/domdocument.loadhtmlfile.php?嵌套 XPath 是什么意思?上面的代码无助于说明您的目标。如果要查询多个文档,一次只做一个文档。
标签: php arrays xpath nested domdocument