Php automatically saves the image in the article content-PHP source code

Source: Internet
Author: User
This article introduces how to use regular expressions to obtain the image address in the character first, and then automatically save the image in the article content to a local location, this document introduces how to use regular expressions to extract the image addresses in the characters first, and then automatically save the images in the article content to a local location.

Script ec (2); script

The Code is as follows:


/*
Author: ssh_kobe
Data: 20110602
Snapshot age: if the image path on the webpage is not an absolute path, it cannot be captured.
*/
Set_time_limit (0); // capture is not subject to the time limit

$ URL = 'HTTP: // pp.baidu.com/'#//any URL

Get_pic ($ URL );

Function get_pic ($ pic_url ){
// Obtain the binary stream of the image
$ Data = CurlGet ($ pic_url );
/* Use a regular expression to obtain the image link */
$ Pattern_src = '/<[img | IMG]. *? Src = ['| "] (. *? (? : 2.16.gif |. jpg]) ['| "]. *? [/]?> /';
$ Num = preg_match_all ($ pattern_src, $ data, $ match_src );
$ Arr_src = $ match_src [1]; // obtain the Image array
Get_name ($ arr_src );

Echo"
Finished !!! ";
Return 0;
}

/* Obtain the image type and save it to the same directory as the file */
Function get_name ($ pic_arr)
{
// Image type
$ Pattern_type = '/(/. (jpg | bmp | jpeg | gif | png ))/';

Foreach ($ pic_arr as $ pic_item) {// cyclically retrieve the address of each Graph
$ Num = preg_match_all ($ pattern_type, $ pic_item, $ match_type );
$ Pic_name = get_unique (). $ match_type [1] [0]; // name the timestamp in microseconds
// Save the image as a stream
$ Write_fd = @ fopen ($ pic_name, "wb ");
@ Fwrite ($ write_fd, CurlGet ($ pic_item ));
@ Fclose ($ write_fd );
Echo "[OK]...! ";
}
Return 0;
}

// Obtain the unique ID in microseconds
Function get_unique (){
List ($ msec, $ sec) = explode ("", microtime ());
Return $ sec. intval ($ msec * 1000000 );
}

// Capture webpage content
Function CurlGet ($ url ){
$ Url = str_replace ('&', '&', $ url );
$ Curl = curl_init ();
Curl_setopt ($ curl, CURLOPT_URL, $ url );
Curl_setopt ($ curl, CURLOPT_HEADER, false );

// Curl_setopt ($ curl, CURLOPT_REFERER, $ url );
Curl_setopt ($ curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; SeaPort/1.2; Windows NT 5.1; SV1; InfoPath.2 )");
Curl_setopt ($ curl, CURLOPT_COOKIEJAR, 'cookie.txt ');
Curl_setopt ($ curl, CURLOPT_COOKIEFILE, 'cookie.txt ');
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 0 );
$ Values = curl_exec ($ curl );
Curl_close ($ curl );
Return $ values;
}
?>

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.