如何使用简单的 dom 解析器从 div class 数据中获取数据?

How to get data from div class data using simple dom parser?

我正在尝试从 class

中获取图片 ID 和描述
<div class="pic" style="top:0px;left:0px;height:149px;" data= 
{"pic_id":"06e50224ab189";}" pic-descr=" this title " data-index="0">

....

</div>

如何查找和获取 pic_id 数据和 pic-descr

这很有趣,因为它看起来 pic_id 坏了 json。我们仍然可以使用正则表达式获取数据:

$str = <<<EOF
<div class="pic" style="top:0px;left:0px;height:149px;" data='{"pic_id":"06e50224ab189";}' pic-descr=" this title " data-index="0">
</div>
EOF;

$html = str_get_html($str);
$div = $html->find('.pic', 0);

if(preg_match('/:"(.*?)"/', $div->data, $m)){
  echo "pic_id is: " . $m[1] . "\n";
}
echo "pic-descr is: " . $div->{'pic-descr'} . "\n";

如果 json 没有损坏,您可以: json_decode($div->data)->pic_id