PHP简单HTML DOM解析器-获取元素';s连字符属性值
我正在使用PHP简单HTML DOM解析器。在我想要解析图像的页面中,“src”属性被替换为“data src”。因此,如果我尝试使用以下代码获取图像路径,它将不返回任何内容:PHP简单HTML DOM解析器-获取元素';s连字符属性值,php,parsing,Php,Parsing,我正在使用PHP简单HTML DOM解析器。在我想要解析图像的页面中,“src”属性被替换为“data src”。因此,如果我尝试使用以下代码获取图像路径,它将不返回任何内容: $elimage = $offer->find('div.photo', 0); $im = $elimage->last_child('a'); $img = $im->last_child('img'); $item['image'] = $img->src; 我尝试过这样做,但也不起作用:
$elimage = $offer->find('div.photo', 0);
$im = $elimage->last_child('a');
$img = $im->last_child('img');
$item['image'] = $img->src;
我尝试过这样做,但也不起作用:
$elimage = $offer->find('div.photo', 0);
$im = $elimage->last_child('a');
$img = $im->last_child('img');
$item['image'] = $img->data-src;
有人知道是否可以获得自定义属性的值吗?如果可以,如何实现
谢谢你的帮助 谢谢你的帮助
我使用以下函数从字符串中获取URL:
function checkURL($str) {
$regex = '$\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]$i';
preg_match_all($regex, $str, $result, PREG_PATTERN_ORDER);
$A = $result[0];
foreach($A as $a) {
$retval .= $a;
}
return $retval;
}
我找到的URL-s是绝对的,在给定的字符串中只有一个URL
再次感谢您的提示@Rajat Singhal
无论如何,我正要找到解决此类问题的方法,在这里遇到了困难,然后突然有了一个想法
<?php
$str= "<a data-src='http://google.com'>Hello</a>";
$var=preg_split("/data-src=\'/",$str);
//echo $var[1];
$var1=preg_split("/\'/",$var[1]);
echo $var1[0];
?>
先把它放在变量中:
$dataSrc='data-src';
$item['image'] = $img->$dataSrc;
而不是
$img->data-src;
使用
摘自Get src by regular expressions form$img感谢您的帮助。使用以下regexp“$\b(https?| ftp |文件)://[-A-Z0-9+&@#/%?=~ |!:,.;]*[-A-Z0-9+&@#/%=~ |]$i”获取源代码。他们有绝对的URL-s。
$img->{'data-src'};