简单 HTML DOM 从 <div> 标签获取值 "data-url"

Simple HTML DOM get value "data-url" from <div> tag

我正在尝试从 "data-url" 从这样的标签中获取值:

<div class="event event-list " data-id="24692" data-url="https://www.example.com/events/20-01-2017-event-x/">

此代码无效:

$urlVariable =  ('https://www.example.com/');

$html           = file_get_html($urlVariable);

foreach($html->find('[data-url]') as $detailLinks)                                              
print $detailLinks . '<br>';    

如有任何帮助,我们将不胜感激。

您也可以通过以下方式使用 domxpath 获得所需的结果:

<?php

$html = '<div class="event event-list " data-id="24692" data-url="https://www.example.com/events/20-01-2017-event-x/">';
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);

print $xpath->query('//div[starts-with(@class,"event")]')->item(0)->getAttribute('data-url');

print $xpath->query('//div[contains(@class,"event-list")]')->item(0)->getAttribute('data-url');

您发现 div 具有 data-url 属性

foreach($html->find('div[data-url]') as $detailLinks) 
   echo $detailLinks->{'data-url'};