如何使用PHP简单HTML Dom获取自定义属性


How to get custom attribute using PHP Simple HTML Dom?

我的HTML 中有带有自定义属性的标签

...
<img alt='test' width='20' height='20' title='test' loadlater='path_to_img.png' />
...

当我使用str_get_html加载代码时,一切都很好。我可以通过DOM导航,我可以搜索。。。但当我从图像标签中读取属性时

foreach($data->find("img") as $_img) {
    print_r($_img->attr);
}

我只得到有效的HTML属性(alt,title,width,height)。缺少属性loadlater

有人能解析自定义属性吗?

我觉得不错。

<?php
require_once "./vendor/autoload.php";
$str = "<img alt=''test'' width=''20'' height=''20'' title=''test'' loadlater=''path_to_img.png'' />";
$dom = 'Sunra'PhpSimple'HtmlDomParser::str_get_html( $str );
foreach($dom->find("img") as $_img) {
    print_r($_img->attr);
}

结果

Array
(
    [alt] => ''test''
    [width] => ''20''
    [height] => ''20''
    [title] => ''test''
    [loadlater] => ''path_to_img.png''
)

作曲家:

"require": {
    "sunra/php-simple-html-dom-parser": "v1.5.0"
}

这对我有效:

$html = <<<EOF
<img alt='test' width='20' height='20' title='test' loadlater='path_to_img.png' />
EOF;
$dom = str_get_html($html);
echo $dom->find('img', 0)->loadlater;