Crawl and download all picture files in CSS PHP code _php tips

Source: Internet
Author: User
Tags mkdir
The bright spot of this article is that the regular-style more complex bird, ╮@@facesymbol@@╭, and then the Copy function of a gray often powerful one usage.
> say just now listen to Nsyta said the theme of small evil white, cups. Recently too busy, not free, or to do a new theme on their own.

A. Crawl a picture in a CSS:
> 1. First, prepare for the job:
> First, the original path to the CSS stored in the $url variables, and then the content of the CSS stored in the ABC.CSS.
> Because of the fact that you often encounter multiple CSS files, it does not fill a CSS path directly.
> Instead, combine the contents of several CSS files together, and plug them all into the Abc.css file, quack.

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

> Then read the contents of the CSS file into the $data variable, and then use the regular type to take out the domain name.
> Because a lot of picture files are used in relative root paths, such as/img/1.gif and Img/1.gif.
> then CSS original address in http://www.jb51.net/css/so the top two file locations are different.

> The first file is/upload/201109/20110926143903807.gif because its path uses a relative root path.
> and the second in/upload/201109/20110926143903169.gif, its path is just a normal relative path.
Copy Code code as follows:

$url = ' http://www.jb51.net/css/'; Preg_match ('/(. *\/\/.*?) \//', $url, $host);
Here use the regular formula to remove the http://www.jb51.net/, the back end do not forget to add a slash.
//.*? is lazy matching, that is, the less matching can match the less content, so that will not overdo it.
$host = $host [1];


2. Set up the Image storage folder:
> Small evil here with the Is_dir to determine whether the folder exists, 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 not.
> But file_exists () is a bit superior because it's been discussed on webmasterworld.com one time.

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

> 3. Use a regular formula to take out the relative address of the picture:

$regex = '/url\ (\ ' {0,1}\ ' {0,1} (. *?) \ ' {0,1}\ ' {0,1}\)/';
//Here is a regular match to the image address, consider three cases, that is, url (1.gif) url (' 1.gif ') url ("1.gif").
//These three types of writing are available, so let's take the 1.gif from the top to get it out.
//\ ' {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. Work with these images:

> First use a loop, which is the first branch of the array to be extracted from the above is processed.
>, the first branch here represents the first bracket in the regular style, hehe, and so on. The

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

> is then judged with a regular, because it also takes into account such/upload/201109/20110926143903807.gif.
> This is done using the full path instead of/img/1.gif or img/1.gif like the others.
> So judge it alone, and then judge the two, and see if it's/img/1.gif or img/1.gif.

Copy Code code as follows:

if (Preg_match ('/^http.*/', $val)) {$target = $val;}
else if (preg_match ('/^\/.*/', $val)) {$target = $host. $val;}
else {$target = $url. $val;}
echo $target. " <br/>\r\n ";

> Finally remove the file name, that is, the 1.gif in the/img/1.gif, for saving files.
Copy Code code as follows:

Preg_match ('/.*\/(. *\.\d+) $/', $val, $name);

> Then we can start downloading, and here's a powerful Copy function usage.
Copy Code code as follows:

if (!is_file ('./img/'. $name [1])) {
$IMGC = file_get_contents ($target);
$handle = fopen ('./img/'. $name [1], ' w+ ');
Fwrite ($handle, $IMGC);
Fclose ($handle);
}

> Top that's our old way, quack, it's troublesome. One time, little evil suddenly found the strong Copy.
> Copy can also download, so you can easily use the following code to deal with the above can be retired birds.
Copy Code code as follows:

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

> 5. Complete source code:

> Fill in the $url when used, and then save all the CSS content in Abc.css.
Copy Code code as follows:

<?php
$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. " <br/>\r\n ";
Preg_match ('/.*\/(. *\.\d+) $/', $val, $name);
if (!is_file ('./img/'. $name [1])) {
Copy ($target, './img/'. $name [1]);
}
}?>

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.