Home >Backend Development >PHP Tutorial >PHP collects images in articles and replaces them locally_PHP tutorial
This article is a detailed analysis and introduction to the local implementation code of image acquisition and replacement in the PHP collection article. Friends who need it can refer to it. Copy code The code is as follows:
/**
* Get the image path in the replacement article
* @param string $xstr content
* @param string $keyword The file name of the created 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 category exists
If(!is_dir($dirslsitss)) {
@mkdir($dirslsitss, 0777);
}
// Match the src of the image
Preg_match_all('#
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 picture to the 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;
}
http://www.bkjia.com/PHPjc/372548.html