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

Php 如何使用简单的dom解析器从div类数据中获取数据?,php,simple-html-dom,Php,Simple Html Dom,我正试图从一个类中获取图片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。我们仍然可以使用正则表达式获取数据:

我正试图从一个类中获取图片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

看起来可能不太像,但这个问题实际上很有启发性。我知道OP标记了这个简单的html dom,但只是确认一下,str_get_html不是本机PHP内置的,对吗?这是其中的一部分吗?