This article mainly for you to share a PHP replacement article in the picture path, download pictures to the local server method, has a good reference value, I hope to help you. Follow the small series together to see it, hope to help everyone.
PHP replaces the image path in the article, downloads the image to the local server
/** * Gets the picture path in the replacement story * @param string $xstr content * @param string $oriweb URL * @return stri ng * */function replaceimg ($xstr, $oriweb) {//save path $d = Date (' Ymd ', Time ()); $saveimgfile _1 = '/uploads/allimg/'. $d; $DIRSLSITSS = dederoot. $saveimgfile _1;//If there is an if (!is_dir ($DIRSLSITSS)) {@mkdir ($DIRSLSITSS, 0777); }//Match the image of 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 picture to Server $fileimgname = Time (). " -". Rand (1000,9999)." JPG "; $filecachs = $dirslsitss. " /". $fileimgname; $fanhuistr = File_put_contents ($filecachs, $img); $saveimgfile = $saveimgfile _1. " /". $fileimgname; $xstr =str_replace ($imgurl, $saveimgfile, $XSTR); }} return $xstr;}