Heim  >  Artikel  >  Backend-Entwicklung  >  php抓取并下载css中所有图片文件

php抓取并下载css中所有图片文件

WBOY
WBOYOriginal
2016-07-25 08:52:29962Durchsuche
  1. if (!is_dir('img')) { mkdir('img'); }
复制代码

> 3、用正则式把图片相对地址取出来:

  1. if (preg_match('/^http.*/',$val)) { $target = $val; }
  2. else if (preg_match('/^\/.*/',$val)) { $target=$host.$val; }
  3. else { $target=$url.$val; }
  4. echo $target."
    \r\n";
复制代码

> 最后把文件名取出来,即 /img/1.gif 中的 1.gif,用于保存文件。

  1. if (!is_file('./img/'.$name[1])) {
  2. $imgc = file_get_contents($target);
  3. $handle = fopen('./img/'.$name[1],'w+');
  4. fwrite($handle,$imgc);
  5. fclose($handle);
  6. }
复制代码

>某次,小邪突然发现 Copy 的强大。 > Copy 居然也可以下载,所以可以轻松使用下面的代码来处理,上面的可以退休鸟。

  1. if (!is_file('./img/'.$name[1])) {
  2. copy($target,'./img/'.$name[1]);
  3. }
复制代码

5、完整源代码: 使用时把 $url 填好即可,然后把所有 CSS 内容存到 abc.css 中即可。

  1. $url = 'http://bbs.it-home.org/css/';
  2. $data = file_get_contents('abc.css');
  3. preg_match('/(.*\/\/.*?)\//',$url,$host);
  4. $host = $host[1];
  5. if (!is_dir('img')) { mkdir('img'); }
  6. $regex = '/url\(\'{0,1}\"{0,1}(.*?)\'{0,1}\"{0,1}\)/';
  7. preg_match_all($regex,$data,$result);
  8. foreach ($result[1] as $val) {
  9. if (preg_match('/^http.*/',$val)) { $target = $val; }
  10. else if (preg_match('/^\/.*/',$val)) { $target=$host.$val; }
  11. else { $target=$url.$val; }
  12. echo $target."
    \r\n";
  13. preg_match('/.*\/(.*\.\D+)$/',$val,$name);
  14. if (!is_file('./img/'.$name[1])) {
  15. copy($target,'./img/'.$name[1]);
  16. }
  17. }?>
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn