[英]Why am I getting an array of SimpleXMLElement Objects here?
我有一些代码从外部源提取HTML:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$xml = @simplexml_import_dom($doc); // just to make xpath more simple
$images = $xml->xpath('//img');
$sources = array();
然后,如果我使用此代码添加所有源:
foreach ($images as $i) {
array_push($sources, $i['src']);
}
echo "<pre>";
print_r($sources);
die();
我得到这个结果:
Array
(
[0] => SimpleXMLElement Object
(
[0] => /images/someimage.gif
)
[1] => SimpleXMLElement Object
(
[0] => /images/en/someother.jpg
)
....
)
但是当我使用这段代码时:
foreach ($images as $i) {
$sources[] = (string)$i['src'];
}
我得到了这个结果(这是你想要的):
Array
(
[0] => /images/someimage.gif
[1] => /images/en/someother.jpg
...
)
是什么造成了这种差异? array_push()有什么不同?
谢谢,
编辑:虽然我明白的答案匹配我问什么(我已经颁发),我更想知道为什么无论是使用array_push或其他符号增加了SimpleXMLElement对象,而不是当两个铸造的arent一个字符串。 我知道什么时候显式地转换为字符串我会得到一个字符串。 请参阅此处的后续问题: 为什么这些值不会作为字符串添加到我的数组中?
差异不是由array_push()
引起的 - 而是由你在第二种情况下使用的类型转换引起的。
在第一个循环中,您正在使用:
array_push($sources, $i['src']);
这意味着您要将SimpleXMLElement
对象添加到数组中。
而在第二个循环中,您正在使用:
$sources[] = (string)$i['src'];
这意味着(由于转换为字符串) , 您正在向数组添加字符串 - 而不再是SimpleXMLElement
对象。
作为参考:手册的相关部分: 类型铸造 。
对不起,刚刚注意到上面有更好的答案,但正则表达式本身仍然有效。 您是否尝试使用HTML标记获取所有图像? 我知道你使用的是PHP,但你可以转换使用这个C#的例子去哪里:
List<string> links = new List<string>();
if (!string.IsNullOrEmpty(htmlSource))
{
string regexImgSrc = @"<img[^>]*?src\s*=\s*[""']?([^'"" >]+?)[ '""][^>]*?>";
MatchCollection matchesImgSrc = Regex.Matches(htmlSource, regexImgSrc, RegexOptions.IgnoreCase | RegexOptions.Singleline);
foreach (Match m in matchesImgSrc)
{
string href = m.Groups[1].Value;
links.Add(href);
}
}
在您的第一个示例中,您应该:
array_push($sources, (string) $i['src']);
第二个示例给出了一个字符串数组,因为您正在使用(string)
强制转换将SimpleXMLElements转换为字符串。 在你的第一个例子中,你不是,所以你得到一个SimpleXMLElements数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.