使用PHP Simple HTML DOM Parser获取Div属性

时间:2014-11-08 14:13:10

标签: php html simple-html-dom

我可以使用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'

感谢。

2 个答案:

答案 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'}));
相关问题