PHP matches the remote image address in the article and downloads the image locally.
Use the regular expression of PHP to implement:
$content = ' Here is the article content, insert a picture test here '; $content = Stripslashes ($content); $img _array = Array ();//matches All remote picture Preg_match_all ("/ (src| SRC) =["|" | {0,} (http://(. *). (gif|jpg|jpeg|bmp|png)) /isu ", $content, $img _array);//matches the non-repeating picture $img_array = Array_unique ($img _array [2]);p Rint_r ($img _array);
It matches the remote picture, and we need to keep it locally. Here are two points to note:
1. Picture save path (Picture storage directory)
2. Actual access to the image address
The following is a complete example: (You can save to local server to modify the appropriate place to test)
<title>PHP Save remote image to local, PHP matches the image address in the article</title><?php//Save the remote image in the article to the local//Author: yanue;//File save directory Path (please change to your own path, you can echo it) $save _path = $_server [' Document_root ']. ' swfupload/attached/';//File save directory Url$save_url = '/swfupload/attached/'; $save _path = Realpath ($save _path). '/';//Picture storage directory $imgpath = $save _path. Date ("Ymd"); $imgUrl = $save _url. Date ("YMD");//Create Folder if (! Is_dir ($imgPath)) {@mkdir ($imgPath, 0777);} $content = ' Here is the article content, insert a picture test here '; $content = Stripslashes ($content); $img _array = Array ();//matches All remote picture Preg_match_all ("/ (src| SRC) =["|" | {0,} (http://(. *). (gif|jpg|jpeg|bmp|png)) /isu ", $content, $img _array);//matches the non-repeating picture $img_array = Array_unique ($img _array [2]);p Rint_r ($img _array);//Time Unlimited set_t Ime_limit (0), foreach ($img _array as $key + $value) {$value = Trim ($value);//read remote picture $get _file = @file_get_co Ntents ($value); Save to local picture name $imgname = Date ("Ymdhis"). '_' . RAND (10000, 99999). "." . SUBSTR ($value,-3, 3); The actual file address (including path and name) saved to local $fileName = $imgPath. '/' . $imgname; The actual access address $fileurL = $imgUrl. "/" . $imgname; The file is written to if ($get _file) {$fp = @fopen ($fileName, "w"), @fwrite ($fp, $get _file), @fclose ($FP);}//Replace the original picture address $con Tent = ereg_replace ($value, $fileurl, $content);} Echo $content;? >
The above is the PHP regular match the remote image address in the article and download the image to the local implementation skills, hope that everyone's learning is helpful.
http://www.bkjia.com/PHPjc/1058162.html www.bkjia.com true http://www.bkjia.com/PHPjc/1058162.html techarticle PHP matches the remote image address in the article and downloads the image locally, using a regular expression of php: $content = ' Here is the article content, insert a picture here to test im ...