[英]Why am I getting an array of SimpleXMLElement Objects here?
I have some code that pulls HTML from an external source: 我有一些代码从外部源提取HTML:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$xml = @simplexml_import_dom($doc); // just to make xpath more simple
$images = $xml->xpath('//img');
$sources = array();
Then, if I add all of the sources with this code: 然后,如果我使用此代码添加所有源:
foreach ($images as $i) {
array_push($sources, $i['src']);
}
echo "<pre>";
print_r($sources);
die();
I get this result: 我得到这个结果:
Array
(
[0] => SimpleXMLElement Object
(
[0] => /images/someimage.gif
)
[1] => SimpleXMLElement Object
(
[0] => /images/en/someother.jpg
)
....
)
But when I use this code: 但是当我使用这段代码时:
foreach ($images as $i) {
$sources[] = (string)$i['src'];
}
I get this result (which is what is desired): 我得到了这个结果(这是你想要的):
Array
(
[0] => /images/someimage.gif
[1] => /images/en/someother.jpg
...
)
What is causing this difference? 是什么造成了这种差异? What is so different about array_push()?
array_push()有什么不同?
Thanks, 谢谢,
EDIT: While I realize the answers match what I am asking (I've awarded), I more wanted to know why whether using array_push or other notation adds the SimpleXMLElement Object and not a string when both arent casted. 编辑:虽然我明白的答案匹配我问什么(我已经颁发),我更想知道为什么无论是使用array_push或其他符号增加了SimpleXMLElement对象,而不是当两个铸造的arent一个字符串。 I knew when explicitly casting to a string I'd get a string.
我知道什么时候显式地转换为字符串我会得到一个字符串。 See follow up question here: Why aren't these values being added to my array as strings?
请参阅此处的后续问题: 为什么这些值不会作为字符串添加到我的数组中?
The difference is not caused by array_push()
-- but by the type-cast you are using in the second case . 差异不是由
array_push()
引起的 - 而是由你在第二种情况下使用的类型转换引起的。
In your first loop, you are using : 在第一个循环中,您正在使用:
array_push($sources, $i['src']);
Which means you are adding SimpleXMLElement
objects to your array. 这意味着您要将
SimpleXMLElement
对象添加到数组中。
While, in the second loop, you are using : 而在第二个循环中,您正在使用:
$sources[] = (string)$i['src'];
Which means (thanks to the cast to string) , that you are adding strings to your array -- and not SimpleXMLElement
objects anymore. 这意味着(由于转换为字符串) , 您正在向数组添加字符串 - 而不再是
SimpleXMLElement
对象。
As a reference : relevant section of the manual : Type Casting . 作为参考:手册的相关部分: 类型铸造 。
Sorry, just noticed better answers above, but the regex itself is still valid. 对不起,刚刚注意到上面有更好的答案,但正则表达式本身仍然有效。 Are you trying to get all images in HTML markup?
您是否尝试使用HTML标记获取所有图像? I know you are using PHP, but you can convert use this C# example of where to go:
我知道你使用的是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);
}
}
In your first example, you should: 在您的第一个示例中,您应该:
array_push($sources, (string) $i['src']);
Your second example gives an array of strings because you are converting the SimpleXMLElements to strings using the (string)
cast. 第二个示例给出了一个字符串数组,因为您正在使用
(string)
强制转换将SimpleXMLElements转换为字符串。 In your first example you are not, so you get an array of SimpleXMLElements instead. 在你的第一个例子中,你不是,所以你得到一个SimpleXMLElements数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.