2013-02-21 41 views
2

,如果我有一個字符串從URL或從HTML獲取所有圖像命名並保存在文件夾

<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」到「/」的遍歷。

我怎麼能找到名稱之間的「/」和「。」這是「www」。

有點混亂。

更新問題:實際的問題

假設我通過PHP從URL得到HTMLcURL()幫助。

我如何檢索所有圖像名稱並存儲在一個文件夾中。

+0

一個正則表達式可能會幫助... – sgibly 2013-02-21 18:21:04

+1

即使你找到第一個,其餘的呢?因爲strpos將返回首次出現的位置... – 2013-02-21 18:22:06

+0

使用適當的SGML/XML解析器獲取src =「xxx」屬性內的部分,然後從那裏開始。 – slugonamission 2013-02-21 18:22:55

回答

6

你可以使用這樣的事情來獲得圖像的來源:

<?php 
    $doc = new DOMDocument(); 
    $doc->loadHTML(htmlstring); 
    $imageTags = $doc->getElementsByTagName('img'); 

    foreach($imageTags as $tag) { 
     echo $tag->getAttribute('src'); 
    } 
?> 
+0

這裏我只是呼應src,但你可以做它你需要的。 – 2013-02-21 18:23:27

+2

+1不建議使用正則表達式。 – slugonamission 2013-02-21 18:24:18

+1

,您可以將該src值放入['pathinfo'](http://php.net/pathinfo)以獲取文件的名稱。 – 2013-02-21 18:24:45

1

您應該使用preg_match_all對於這樣的任務。未測試:

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

var_dump($matches);

$matches現在應該包含WWW,TTT,存款準備金率。

+1

永遠不要試圖用正則表達式解析HTML。 HTML不是常規語言,不能用正則表達式解析。改用適當的HTML/XML解析器。有關詳細信息,請參閱http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – 2013-02-22 09:50:24

1
$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 
     ) 

) 
0

我在大家的幫助下寫了一個腳本。 希望這可以幫助許多解決方案尋求者解決我的問題。

<?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); 
     } 
    ?> 
相關問題