PHP code for crawling and downloading all image files in CSS _php tutorial

Source: Internet
Author: User
The highlight of this article is that the regular-style more complex bird, ╮@@facesymbol@@╭, and then the Copy function of the gray often powerful one usage.
> said just now listen to Nsyta that the theme of Small evil White, cup with. Recently too busy, not free, or I will make a new theme.

One. Crawl the picture in the CSS:
> 1. First, prepare for the job:
> First, the original CSS path is stored in the $url variable, and then the content of the CSS is saved in Abc.css.
> Because of the situation that often encountered multiple CSS files, so little evil does not directly fill a CSS path.
> Instead, merge the contents of several CSS files together and plug them into the Abc.css file, quack.

$data = file_get_contents (' abc.css ');

> then reads the contents of the CSS file into the $data variable, then uses the regular type to take out the domain name.
> Because there are a lot of picture files that take the relative root path, say/img/1.gif and img/1.gif.
> then the CSS original address in http://www.jb51.net/css/so the above two file locations are different.

> The first file is in/upload/201109/20110926143903807.gif because its path is used relative to the root path.
> and the second one in/upload/201109/20110926143903169.gif, its path is just the normal relative path.
Copy CodeThe code is as follows:
$url = ' http://www.jb51.net/css/'; Preg_match ('/(. *\/\/.*?) \//', $url, $host);
Here use the regular formula to take out the http://www.jb51.net/, the back end do not forget to add a slash oh.
//.*? is a lazy match, that is, the less you can match the less the content, so you don't overdo it.
$host = $host [1];


2. Create a picture Storage folder:
> Small evil here Is_dir is used to determine if a folder exists, and there is no need to build a second time.
> Oh, by the way, the Is_file function can determine whether this file is a normal file, or whether it exists.
> But file_exists () is a bit superior, because some people have been discussing it on webmasterworld.com.

if (!is_dir (' img ')) {mkdir (' img ');}

> 3. Take the image relative to the address in regular style:

$regex = '/url\ (\ ' {0,1}\ ' {0,1} (. *?) \ ' {0,1}\ ' {0,1}\)/';
Here, you can match the image address with regular style, consider three cases, namely URL (1.gif) url (' 1.gif ') url ("1.gif").
These three kinds of writing are all available, so let's use the above-mentioned 1.gif to take out the inside.
\ ' {0,1} indicates that single quotes can occur 1 or 0 times, \ ' means that double quotes can occur 1 or 0 times.
The middle must use lazy match, otherwise take out is 1.gif "instead of 1.gif Bird, O (∩_∩) P.
Preg_match_all ($regex, $data, $result);

> 4. Process these pictures:

> First use a loop to handle the first branch content array that is extracted with the regular.
>, the first branch here represents the first parenthesis in the regular style, and so on.

foreach ($result [1] as $val) {}

> is then judged by the regular formula, because it also takes into account such/upload/201109/20110926143903807.gif.
> This is the use of the complete path, rather than the other is the same/img/1.gif or img/1.gif.
> So judge it alone, then judge the two, and see if it's/img/1.gif or img/1.gif.
Copy CodeThe code is as follows:
if (Preg_match ('/^http.*/', $val)) {$target = $val;}
else if (preg_match ('/^\/.*/', $val)) {$target = $host. $val;}
else {$target = $url. $val;}
echo $target. "
\ r \ n ";

> finally take out the file name, that is, 1.gif in/img/1.gif, for saving files.
Copy CodeThe code is as follows:
Preg_match ('/.*\/(. *\.\d+) $/', $val, $name);

> Then we can start the download, and here's a powerful Copy function usage.
Copy CodeThe code is as follows:
if (!is_file ('./img/'. $name [1])) {
$IMGC = file_get_contents ($target);
$handle = fopen ('./img/'. $name [1], ' w+ ');
Fwrite ($handle, $IMGC);
Fclose ($handle);
}

> The above is our old method, quack, very troublesome. Some time, small evil suddenly found Copy of the powerful.
> Copy Incredibly can also download, so you can easily use the following code to handle, above the can retire bird.
Copy CodeThe code is as follows:
if (!is_file ('./img/'. $name [1])) {
Copy ($target, './img/'. $name [1]);
}

> 5. Full Source code:

> Fill in the $url when you use it, and then save all CSS content to abc.css.
Copy CodeThe code is as follows:
$url = ' http://www.jb51.net/css/';
$data = file_get_contents (' abc.css ');
Preg_match ('/(. *\/\/.*?) \//', $url, $host);
$host = $host [1];
if (!is_dir (' img ')) {mkdir (' img ');}
$regex = '/url\ (\ ' {0,1}\ ' {0,1} (. *?) \ ' {0,1}\ ' {0,1}\)/';
Preg_match_all ($regex, $data, $result);
foreach ($result [1] as $val) {
if (Preg_match ('/^http.*/', $val)) {$target = $val;}
else if (preg_match ('/^\/.*/', $val)) {$target = $host. $val;}
else {$target = $url. $val;}
echo $target. "
\ r \ n ";
Preg_match ('/.*\/(. *\.\d+) $/', $val, $name);
if (!is_file ('./img/'. $name [1])) {
Copy ($target, './img/'. $name [1]);
}
}?>

http://www.bkjia.com/PHPjc/324413.html www.bkjia.com true http://www.bkjia.com/PHPjc/324413.html techarticle The highlight of this article is that the regular-style more complex bird, ╮@@facesymbol@@╭, and then the Copy function of the gray often powerful one usage. Words just listen to Nsyta said the theme of Small evil White, cup with. ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.