In this article, we 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 help from other websites.
In this article, we 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 help from other websites.
As a senior and professional suede engineer, 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 __). 'http: // www.jb51.net /'. $ 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, 'HTTP: // www.jb51.net/'); // 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.