If you use php to collect articles instead of using tools, if the addresses in the articles are not replaced by the addresses of other websites, the following describes how to use the preg_match_all function to occasionally download and replace the image address in the collected content. if you use php to collect articles instead of using tools, if the addresses in the articles are not replaced by the addresses of other websites, the following describes how to use the preg_match_all function to occasionally download and replace the image address in the collected content.
Script ec (2); script
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 (! 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; } |