Php Regular Expression matches the remote image address in the article and downloads the image to the local device.

Source: Internet
Author: User
Tags php regular expression

Today, a simple collection program needs to download the content of the website of the other party, and then store the images in the content on the local server. Next I will introduce you to my specific operation methods, the file_get_contents function is used to download images. The method is as follows.


Here we use the php regular expression to implement:

The Code is as follows: Copy code
$ Content = 'here is the article content, 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)

The Code is as follows: Copy code

<! Doctype html>
<Html lang = "en-US">
<Head>
<Meta charset = "UTF-8">
<Title> php saves the remote image to the local device, and php RegEx matches the image address in the article. </title>
</Head>
<Body>
<? 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. You can echo it)
$ Save_path = $ _ SERVER ['document _ root']. 'swfupload/attached /';
// File Save directory URL
$ 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 article content, 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 );
// Unlimited time
Set_time_limit (0 );
Foreach ($ img_array as $ key => $ value ){
$ Value = trim ($ value );
// Read remote Images
$ Get_file = @ file_get_contents ($ value );
// Save it to the 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;
// Actual access address
$ Fileurl = $ imgUrl. "/". $ imgname;
// File writing
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;
?>
</Body>
</Html>


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.