我们可以在DomDocument中使用Array Element并嵌套xpath吗?
我使用以下代码
$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 中插入数组元素 aslo,因为我将 domdocument 嵌套在 domdocument 中,所以我是 声明新的 xpath 查询
问题是...
澄清:
I am using the following code
$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;
}
}
so the scenerio is ,
I am nesting a 'domdocument' inside an other 'DomDocument' using 'foreach loop', i am inserting Array Element in DomDocument aslo, as i am nestind domdocument inside domdocument , so i am
declaring new xpath query
The Question is ...
Clarification:
$title_links is obtained using curl, it is urls
extracted from web page using domdocument and xpath query.so
$title_links is at first level of xpath and domdocument, and $nodelist
is at the 2nd level of nesting od xpath and dom-document
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
失败的原因是:
@$dom2->loadHTML($page_data[]);
[]
只能用于赋值,不能用于读取。您实际上需要有一个可以参考的密钥。我可能会这样做:我仍然不清楚这里到底是如何发生嵌套的;你没有嵌套任何东西,你只是迭代一个数组。
The reason it's failing is this:
@$dom2->loadHTML($page_data[]);
[]
can only be used for assignment, not reading. You need to actually have a key you can reference. I would probably do it like this:What's still unclear to me is how exactly there is any nesting going on here; you're not nesting anything, you are just iterating over an array.