我可以使用以下代码使用 PHP Simple HTML DOM Parser
获取 anchor 的属性 href
foreach($page->find('a') as $anchor){
echo trim(strip_tags($anchor->href));
}
这工作得非常好。
但这是问题,现在我必须获取 div
data-cursor
属性
<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'
谢谢。
最佳答案
Error: Use of undefined constant cursor - assumed 'cursor'
这应该告诉您,由于连字符的原因,PHP 无法正确解析您的代码。它正在读取“数据”,然后连字符将其丢弃。尝试像这样包围它:
$div->{'data-cursor'}
关于php - 使用 PHP 简单 HTML DOM 解析器获取 Div 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26817985/