从url或html中获取所有图像的名称并保存在文件夹中


Get all images name from a url or from html and save in folder

如果我有一个字符串

<div> balah balah <img src='image/www.png' /> balah balah</div>
<div> balah balah <img src='image/ttt.png' /> balah balah</div>
<div> balah balah <img src='image/rrr.png' /> balah balah</div>

我怎么能在src中找到图像名称呢。我使用这个代码

 $pos = strpos($srt,".png");

为了找到.png的位置,我得到了位置。

我找到了第一个".png",但没有找到从".png"到"/"返回的任何方法。

我怎么能找到介于"/"answers"."之间的名字,也就是"www"。

有点困惑。

更新问题:实际问题

假设我在cURL()的帮助下通过PHP从URL获得HTML

如何检索所有图像的名称并存储在文件夹中。

您可以使用类似的东西来获取图像的来源:

<?php
    $doc = new DOMDocument();
    $doc->loadHTML(htmlstring);
    $imageTags = $doc->getElementsByTagName('img');
    foreach($imageTags as $tag) {
        echo $tag->getAttribute('src');
    }
?>

对于这样的任务,应该使用preg_match_all。未测试:

preg_match_all('/image'/(.*)'.png/iU', $str, $matches);

var_dump($matches);

$matches现在应该包含www、ttt、rrr。

$text = "
<div> balah balah <img src='image/www.png' /> balah balah</div>
<div> balah balah <img src='image/ttt.png' /> balah balah</div>
<div> balah balah <img src='image/rrr.png' /> balah balah</div>
";
preg_match_all("/src='image'/([^.]+)/i", $text, $out);
/*
echo $out[1][0]; //www
echo $out[1][1]; //ttt
echo $out[1][2]; //rrr
*/
print_r($out);
OUTPUT
Array
(
    [0] => Array
        (
            [0] => src='image/www
            [1] => src='image/ttt
            [2] => src='image/rrr
        )
    [1] => Array
        (
            [0] => www
            [1] => ttt
            [2] => rrr
        )
)

我在大家的帮助下编写了一个脚本。希望这将帮助许多像我的问题一样寻求解决方案的人。

  <?php
        $url='http://php.net/'; 
        $returned_content = get_url_contents($url); 
        /* gets the data from a URL */
        function get_url_contents($url){
                $crl = curl_init();
                $timeout = 5;
                curl_setopt ($crl, CURLOPT_URL,$url);
                curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
                $ret = curl_exec($crl);
                curl_close($crl);
                return $ret;
        }
        $doc = new DOMDocument();
        $doc->loadHTML($returned_content);
        $imageTags = $doc->getElementsByTagName('img');
        $img1 = array();
        foreach($imageTags as $tag) {
            $img1[] = $tag->getAttribute('src');
        }
        foreach($img1 as $i){
            save_image($i);
            if(getimagesize(basename($i))){
                echo '<h3 style="color: green;">Image ' . basename($i) . ' Downloaded OK</h3>';
            }else{
                echo '<h3 style="color: red;">Image ' . basename($i) . ' Download Failed</h3>';
            }
        }
        //Alternative Image Saving Using cURL seeing as allow_url_fopen is disabled - bummer
        function save_image($img1,$fullpath='http://example.com/'){
            if($fullpath=='http://example.com/'){
                $fullpath = basename($img1);
            }
            $ch = curl_init ($img1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
            $rawdata=curl_exec($ch);
            curl_close ($ch);
            if(file_exists($fullpath)){
                unlink($fullpath);
            }
            $fp = fopen($fullpath,'x');
            fwrite($fp, $rawdata);
            fclose($fp);
        }
    ?>