Copy Code code as follows:
/**
* Get a picture path in a replacement article
* @param string $xstr content
* @param string $keyword Create a file name for a 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);
}
src for matching pictures
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 pictures 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;
}