【问题标题】:PHP - How to get all urls from many img's src?PHP - 如何从许多 img 的 src 中获取所有 url?
【发布时间】:2017-06-04 17:01:00
【问题描述】:

假设我们有这样的字符串:

Its really great to <img src="image2.png" /> hear from you "Today is good <img src="http://www.google.com/picture2.png" /> day" Let's listen song together! ---------<img src="images/profile.png" />\\\\\\

这是整个字符串。我们里面有 3 张图片。

我们想从这个字符串中产生变量

output[0] = 'image2.png';
output[1] = 'http://www.google.com/picture2.png';
output[2] = 'images/profile.png';

我的意思是,我们有这个字符串,以及如何处理他以从 img 标签中提取所有“src”并将其收集到一个新的数组变量中。

怎么做?我们怎样才能做到这一点?

另外我使用 CodeIgniter 框架。也许只用这个框架的方法就可以完成,但我认为不可能。

【问题讨论】:

    标签: php string codeigniter frameworks


    【解决方案1】:

    使用preg_match_all():

    $src = <<<EOL
    Its really great to <img src="image2.png" /> hear from you "Today is good
    <img src="http://www.google.com/picture2.png" /> day" Let's listen song
    together! ---------<img src="images/profile.png" />\\\\\\
    EOL;
    
    preg_match_all('~src="([^"]+)~', $src, $matches);
    
    var_export($matches[1]);
    // output ->
    //        array (
    //          0 => 'image2.png',
    //          1 => 'http://www.google.com/picture2.png',
    //          2 => 'images/profile.png',
    //        )
    

    直播demo


    更新:您可以在正则表达式模式中使用\K 来获得$matches 中所需的内容:

    preg_match_all('~src="\K[^"]+~', $src, $matches);
    var_export($matches);
    // output ->
    //      array (
    //        0 =>
    //        array (
    //          0 => 'image2.png',
    //          1 => 'http://www.google.com/picture2.png',
    //          2 => 'images/profile.png',
    //        ),
    //      )
    

    如需参考,请参阅Escape sequences

    【讨论】:

    • 为什么是var_export($matches[1]);?为什么是1?为什么这会产生 2 行而不是 1 行?
    • $matches[0] 包含与完整模式 src="([^"]+) 匹配的字符串数组。 $matches[1] 包含第一个子掩码匹配的数组:([^"]+)
    【解决方案2】:

    在整个页面的源代码上使用preg_match_all ( string $pattern , string $subject [, array &amp;$matches 来挑选 src= 值。像这样:

    $src = array (); // array for src's
    preg_match_all ( '/src="([^"]+)"/', $page_source, $src );
    $just_urls = $src [1];
    

    $page_source 是您的输入,$srcsrc= 值的结果数组,$just_urls 是引号内的数组。

    /src="([^"]+)"/ 模式将只返回引号内的内容。

    见: https://secure.php.net/manual/en/function.preg-match-all.php

    【讨论】:

    • 很好,但不能完全正确。对此进行了测试,请在此处查看结果:https://i.stack.imgur.com/oQFrL.png。它给出了好的结果和坏的结果,并且是 2 行而不是 1 行。
    • 哦,你必须使用第二个数组来匹配。我将编辑代码。
    【解决方案3】:

    您需要使用PHP DOM Extension。 DOM 扩展允许您使用 PHP 通过 DOM API 对 XML 文档进行操作。

    你也可以通过下面的代码:

    function fetchImages($content) {
        $doc = new DOMDocument(); 
        $doc->loadHTML($content);
        $imgElements = $doc->getElementsByTagName('img');
    
        $images = array();
    
        for($i = 0; $i < $imgElements->length; $i++) {
            $images[] = $imgElements->item($i)->getAttribute('src');
        }
    
        return $images;
    }
    $content = file_get_contents('http://www.example.com/');
    $images = fetchImages($content);
    
    print_r($images);
    

    【讨论】:

    • 很好。但我需要做我的 PHP 伙伴:D
    • 我可以确切地知道需求吗?这样我可以进一步解释:)
    • 下面的答案完全符合我的要求。一切都清楚,简短,在 PHP 中。这里:https://i.stack.imgur.com/oQFrL.png 但是有一点不对
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    相关资源
    最近更新 更多