我正在尝试从课程中获取图片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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句