Use PHP to download images in CSS files

Source: Internet
Author: User
This article will share with you the function of downloading images in CSS files through regular expression matching in PHP. the code is very simple to use, especially for children who frequently seek others' websites as senior and professional pickpockets, I have accumulated rich experience in the Internet since I started my junior year. I believe that every web programmer will have similar experiences.

You must download the images in the style file. If you encounter a large style file that may contain hundreds of images to be downloaded, the following small code is the most suitable.

<? Php/* More & Original PHP Framwork Copyright (c) 2007-2008 IsMole Inc. author: kimi Documentation: download the image in the style file. this is a utilities for water and water. * // note: Set the PHP timeout time to set_time_limit (0 ); // note: get the style file content $ styleFileContent = file_get_contents ('images/style.css '); // The note matches the URL preg_match_all ("/url \((. *) \)/", $ styleFileContent, $ imagesURLArray); // download the address to be downloaded from the note loop one by one $ imagesURLArray = array_unique ($ imagesURLArray [1]); foreach ($ imagesURLArray as $ imagesURL) {file_put_contents (basename ($ imagesURL), file_get_contents ($ imagesURL ));}

I will share with you a encapsulated class.

/*** Get the image address in CSS, and save the image to the local */class getInCssImage {/*** save the image * @ param $ cssUrl css url * @ param $ dir save the image directory * @ return void */ static public function saveImage ($ cssUrl, $ dir) {$ content = file_get_contents ($ cssUrl); $ patterns = '/images (. *). (jpg | gif | png)/'; // the regular expression must be preg_match_all ($ patterns, $ content, $ matches) based on different addresses; $ imagesUrls = $ matches [0]; if (! Is_dir ($ dir) mkdir (dirname (_ FILE __). '/'. $ dir, 0777); foreach ($ imagesUrls as $ image) {ob_start (); $ imageUrl = "http://www.xx.com /". $ image; // This address was originally obtained by a program. Lazy readfile ($ imageUrl); $ img = ob_get_contents (); ob_end_clean (); $ size = strlen ($ img); $ localImage = $ dir. strchr ($ image, '/'); // The image address saved locally $ fp = fopen ($ localImage, 'A'); fwrite ($ fp, $ img ); fclose ($ fp) ;}}}$ content = getInCssImage: saveImage ('/css/css.css', 'image ');

Finally, I wish you all the best in the process of skin peeling!

In addition, the definition and usage of file_put_contents are attached.

The file_put_contents () function writes a string to a file.
It is the same as calling the fopen (), fwrite (), and fclose () functions in turn.

Syntax

File_put_contents (file, data, mode, context)

Parameter description

File is required. Specifies the file to write data. If the file does not exist, create a new file.
Data is optional. Specifies the data to be written into the file. It can be a string, array, or data stream.
Mode
Optional. Specifies how to open/write files. Possible values:
FILE_USE_INCLUDE_PATH
FILE_APPEND
LOCK_EX
Context
Optional. Specifies the file handle environment.
Context is a set of options that can modify the behavior of a stream. If null is used, this parameter is ignored.

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.