为什么我在这里得到 SimpleXMLElement 对象数组?

发布于 2024-11-02 02:27:33 字数 1281 浏览 4 评论 0原文

我有一些从外部源提取 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 对象而不是字符串没有铸造。我知道当显式转换为字符串时我会得到一个字符串。请参阅此处的后续问题:为什么不这些值作为字符串添加到我的数组中?

I have some code that pulls HTML from an external source:

$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()?

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. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我不会写诗 2024-11-09 02:27:33

差异不是由 array_push() 引起的,而是由您在第二种情况下使用的类型转换引起的。

In your first loop, you are using :

array_push($sources, $i['src']);

这意味着您正在向数组添加SimpleXMLElement 对象

While, in the second loop, you are using :

$sources[] = (string)$i['src'];

这意味着(由于转换为字符串)您正在将字符串添加到数组中,而不再是 SimpleXMLElement 对象。

As a reference : relevant section of the manual : [**Type Casting**][1].

The difference is not caused by array_push() -- but by the type-cast you are using in the second case.

In your first loop, you are using :

array_push($sources, $i['src']);

Which means you are adding SimpleXMLElement objects to your array.

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.

As a reference : relevant section of the manual : [**Type Casting**][1].

醉南桥 2024-11-09 02:27:33

抱歉,刚刚注意到上面有更好的答案,但正则表达式本身仍然有效。
您是否想获取 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);
                }

        }

Sorry, just noticed better answers above, but the regex itself is still valid.
Are you trying to get all images in HTML markup?
I know you are using PHP, but you can convert use this C# example of where to go:

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);
                }

        }
梦途 2024-11-09 02:27:33

在第一个示例中,您应该:

array_push($sources, (string) $i['src']);

第二个示例给出了一个字符串数组,因为您正在使用 (string) 转换将 SimpleXMLElements 转换为字符串。在您的第一个示例中,您不是,因此您会得到一个 SimpleXMLElements 数组。

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. In your first example you are not, so you get an array of SimpleXMLElements instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文