Php downloads the image address from the collected content and replaces the local address. _ PHP Tutorial

Source: Internet
Author: User
Php downloads the image address from the collected content and replaces the local address ,. Php downloads and replaces the image address in the collected content with the local address, and obtains all the addresses in the string to an array. We use the preg_match_all function code to copy the code as follows? Php $ php downloads the image address from the collected content and replaces the local address,

Get all the addresses in the string to an array. We use the preg_match_all function.

The code is as follows:
$ Str ='

';
$ Pattern = "/<[img | IMG]. *? Src = ['| "] (. *? (? : 2.16.gif |. jpg]) ['| "]. *? [/]?> /";
Preg_match_all ($ pattern, $ str, $ match); print_r ($ match );
?>

Result

Array
(
[0] => Array
(
[0] =>
)
[1] => Array
(
[0] => upfiles/2009/07/1246430143_1.jpg
)
)

With the core functions above, we can do the following.

The code is as follows:
/**
* Obtain the image path in the replacement document.
* @ Param string $ xstr content
* @ Param string $ name of the photo file created by keyword
* @ Param string $ oriweb URL
* @ Return string
*
*/
Function replaceimg ($ xstr, $ keyword, $ oriweb ){

// Save path
$ D = date ('ymmd', time ());
$ Dirslsitss = '/var/www/weblist/uploads/'. $ keyword. '/'. $ d; // whether the category exists
If (www.111cn.net )(! Is_dir ($ dirslsitss )){
@ Mkdir ($ dirserver load balancer, 0777 );
}

// Match the image's src
Preg_match_all ('#] *> # I', $ xstr, $ match );

Foreach ($ match [1] as $ imgurl ){

$ Imgurl = $ imgurl;

If (is_int (strpos ($ imgurl, 'http '))){
$ Arcurl = $ imgurl;
} Else {
$ Arcurl = $ oriweb. $ imgurl;
}
$ Img = file_get_contents ($ arcurl );


If (! Empty ($ img )){

// Save the image to the server
$ Fileimgname = time (). "-". rand (, 9999). ". jpg ";
$ Filecachs = $ dirslsitss. "/". $ fileimgname;
$ Fanhuistr = file_put_contents ($ filecachs, $ img );
$ Saveimgfile = "/uploads/$ keyword". "/". $ d. "/". $ fileimgname;


$ Xstr = str_replace ($ imgurl, $ saveimgfile, $ xstr );
}
}
Return $ xstr;
}

From: http://www.111cn.net/phper/php-cy/48607.htm




Returns all the addresses in the string to an array. the code using the preg_match_all function is as follows? Php $...

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.