Php regular expression matches the remote image address in the article and downloads the image to the local device ,. Php regular expression matches the remote image address in the article and downloads the image to the local device. it is implemented using the php regular expression: $ content the content of the article is here, insert an image here to test the remote image address in the im php regular expression matching article and download the image to your local device,
Use the php regular expression to implement:
$ Content = 'here is the content of the article. insert an image here to test '; $ content = stripslashes ($ content); $ img_array = array (); // match all remote images preg_match_all ("/(src | SRC) = [" | '|] {0,} (http ://(. *). (gif | jpg | jpeg | bmp | png)/isU ", $ content, $ img_array ); // matched non-repeated images $ img_array = array_unique ($ img_array [2]); print_r ($ img_array );
The above will match the remote image, and we need to keep it locally. Note the following two points:
1. image storage path (image storage directory)
2. actually accessing the image address
The following is a complete example: (you can save it to the local server and modify the corresponding places for testing)
Php saves the remote image to the local device. The php regular expression matches the image address in the article.<? Php // Save the remote image in the article to the local device // Author: yanue; // Save the directory path of the file (replace it with your own path, and you can echo it) $ save_path = $ _ SERVER ['document _ root']. 'swfupload/attached/'; // URL of the file storage directory $ save_url ='/swfupload/attached/'; $ save_path = realpath ($ save_path ). '/'; // image storage directory $ imgPath = $ save_path. date ("Ymd"); $ imgUrl = $ save_url. date ("Ymd"); // create a folder if (! Is_dir ($ imgPath) {@ mkdir ($ imgPath, 0777);} $ content = 'here is the content of the article, insert an image here for testing '; $ content = stripslashes ($ content); $ img_array = array (); // match all remote images preg_match_all ("/(src | SRC) = ["| '|] {0,} (http ://(. *). (gif | jpg | jpeg | bmp | png)/isU ", $ content, $ img_array ); // matched non-repeated images $ img_array = array_unique ($ img_array [2]); print_r ($ img_array); // set_time_limit (0) with unlimited time ); foreach ($ img_array as $ key => $ v Alue) {$ value = trim ($ value); // read the remote image $ get_file = @ file_get_contents ($ value ); // save to local image name $ imgname = date ("YmdHis "). '_'. rand (10000,999 99 ). ". ". substr ($ value,-3, 3); // the actual file address (including the path and name) saved locally $ fileName = $ imgPath. '/'. $ imgname; // The actual access address $ fileurl = $ imgUrl. "/". $ imgname; // if ($ get_file) {$ fp = @ fopen ($ fileName, "w"); @ fwrite ($ fp, $ get_file); @ fclose ($ Fp);} // replace the original image address $ content = ereg_replace ($ value, $ fileurl, $ content);} echo $ content;?>
The above is the implementation tips for php regular expression matching to download remote image addresses and images to a local device. I hope this will be helpful for your learning.
Using php regular expressions: $ content = 'here is the article content, insert an image here to test im...