PHP regular matches the remote picture address in the article and downloads the image to the local, _php tutorial

Source: Internet
Author: User

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 ...

  • 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.