[英]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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.