简单的HTML DOM获得价值&#34; data-url&#34;来自<div>标签

时间:2018-01-06 16:38:16

标签: php simple-html-dom

我试图从&#34; data-url&#34;中获取价值。来自这样的标签:

<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>';    

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

您也可以通过以下方式使用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');

答案 1 :(得分:0)

您可以找到带有data-url属性的div

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