我可以使用href
使用以下代码获取锚点的属性PHP Simple HTML DOM Parser
foreach($page->find('a') as $anchor){
echo trim(strip_tags($anchor->href));
}
这绝对没问题。
但问题是,现在我要获得data-cursor
div
属性
<div class = 'someClass' data-cursor = '4515314844'>
some contents here..
</div>
但如果我尝试相同的方法,如上所述,这会引发错误
foreach($page->find('div') as $div){
echo trim(strip_tags($div->data-cursor));
}
Error: Use of undefined constant cursor - assumed 'cursor'
感谢。
答案 0 :(得分:2)
Error: Use of undefined constant cursor - assumed 'cursor'
那应该告诉你PHP因为连字符而没有正确解析你的代码。它正在读取“数据”然后连字符将其丢弃。尝试围绕它:
$div->{'data-cursor'}
答案 1 :(得分:1)
要正确访问高级财产,您需要这样做:
$div->{'data-cursor'}
看起来像这样:
echo trim(strip_tags($div->{'data-cursor'}));