This article is the PHP acquisition of the article in the image acquisition replacement to the local implementation code for a detailed analysis of the introduction, the need for friends under the reference
copy code code as follows:
/**
* Gets the path to replace the picture in the article
* @param string $xstr content
* @param string $keyword Create a file name for the photo
* @param string $oriweb URL
* @return String
*
*/
function replaceimg ($xstr, $keyword, $oriweb) {
//save path
$d = Date (' Ymd ', Time ());
$dirslsitss = '/var/www/weblist/uploads/'. $keyword. ' /'. $d;//Whether the classification exists
if (!is_dir ($DIRSLSITSS)) {
@mkdir ($DIRSLSITSS, 0777);
}
//matching picture of Src
Preg_match_all (' #<img.*?src= "([^"]*) "[^>]*> #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 = "/uploads/$keyword". /". $d." /". $fileimgname;
$xstr =str_replace ($imgurl, $saveimgfile, $XSTR);
}
}
return $xstr;
}