I wrote this function today and shared it with my php100 friends.
Step 1. Extract all from the article using the regular expression.
$ Message // document content
// Regular (this is not yet)
$ Reg = "/] * src =" (http ://(. + )/(. + ). (jpg | gif | bmp | bnp ule) "/isU ";
// Store the extracted img address in the $ img_array variable.
Preg_match_all ($ reg, $ message, $ img_array, PREG_PATTERN_ORDER );
// Filter duplicate Images
$ Img_array = array_unique ($ img_array [1]);
[/Php]
Step 2. loop through the $ img_array. Save the image and replace the position of the article.
[Php]
Foreach ($ img_array as $ img ){
// Determine whether it is an image on your website
If (xxx.com! = Get_domain ($ img) {// if this image is not on your server
// Read image files
$ Gimg = new GetImage ();
$ Gimg-> source = $ img;
$ Gimg-> save_to =./data/temp /;
$ FILE = $ Gimg-> download (); // move the image to the local device.
// Save the image to the position where the image is saved.
$ Img_path = pic_save ($ FILE, 0 ,);
// Text path replacement
$ Message = str_replace ($ img, $ img_path, $ message );
}
}
... At this time, the image in $ message has been replaced with the local address of your server, and the image is saved to your server.
[/Php]
[Php]
// The following functions and classes are found on the network.
// Obtain the domain name from the url
Function get_domain ($ url ){
$ Pattern = "/[w-] +. (com | net | org | gov | cc | biz | info | cn) (. (cn | hk ))*/";
Preg_match ($ pattern, $ url, $ matches );
If (count ($ matches)> 0 ){
Return $ matches [0];
} Else {
$ Rs = parse_url ($ url );
$ Main_url = $ rs ["host"];
If (! Strcmp (long2ip (sprintf ("% u", ip2long ($ main_url), $ main_url )){
Return $ main_url;
} Else {
$ Arr = explode (".", $ main_url );
$ Count = count ($ arr );
$ EndArr = array ("com", "net", "org", "3322"); // com.cn net.cn
If (in_array ($ arr [$ count-2], $ endArr )){
$ Domain = $ arr [$ count-3]. ".". $ arr [$ count-2]. ".". $ arr [$ count-1];
} Else {
$ Domain = $ arr [$ count-2]. ".". $ arr [$ count-1];
}
Return $ domain;
} // End if (! Strcmp ...)
} // End if (count ...)
} // End function