Home > php教程 > php手册 > php采集文章中的图片获取替换到本地(实现代码)

php采集文章中的图片获取替换到本地(实现代码)

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:28:31
Original
1171 people have browsed it

本篇文章是对php采集文章中的图片获取替换到本地的实现代码进行了详细的分析介绍,需要的朋友参考下

复制代码 代码如下:


/**
 * 获取替换文章中的图片路径
 * @param string $xstr 内容
 * @param string $keyword 创建照片的文件名
 * @param string $oriweb 网址
 * @return string
 *
 */
function replaceimg($xstr,$keyword, $oriweb){

    //保存路径
    $d = date('Ymd', time());
    $dirslsitss = '/var/www/weblist/uploads/'.$keyword.'/'.$d;//分类是否存在
    if(!is_dir($dirslsitss)) {
        @mkdir($dirslsitss, 0777);
    }

    //匹配图片的src
    preg_match_all('#php采集文章中的图片获取替换到本地(实现代码)]*>#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)) {

            //保存图片到服务器
            $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;
}

,香港服务器,美国服务器,香港服务器
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template