Home  >  Article  >  Backend Development  >  PHP grabs and downloads all image files in css

PHP grabs and downloads all image files in css

WBOY
WBOYOriginal
2016-07-25 08:52:291011browse
  1. if (!is_dir('img')) { mkdir('img'); }
Copy code

> 3. Use regular expressions to get the relative address of the image:

  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."
    rn";
Copy code

> Finally Take out the file name, which is 1.gif in /img/1.gif, and use it to save the file.

  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 code

>One time, Xiaoxie suddenly discovered the power of Copy. > Copy can actually be downloaded, so you can easily use the following code to handle it, and the above one can retire the bird.

  1. if (!is_file('./img/'.$name[1])) {
  2. copy($target,'./img/'.$name[1]);
  3. }
Copy code

5. Complete source code: Just fill in $url when using it, and then save all CSS content to 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."
    rn";
  13. preg_match('/.*/(.*.D+)$/',$val,$name);
  14. if (!is_file('./img/'.$name[1])) {
  15. copy($ target,'./img/'.$name[1]);
  16. }
  17. }?>
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn