Home  >  Article  >  Backend Development  >  PHP regular extracts images from articles, and replaces and moves the image directory_PHP tutorial

PHP regular extracts images from articles, and replaces and moves the image directory_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:57:51868browse

This article will list some commonly used PHP regular rules to extract images from articles, and replace and move image directory codes. I hope this article will be helpful to everyone.

When uploading pictures, if not all the uploaded pictures are used, you can set it to only save it in a temporary folder when uploading. When the article is published, move the actually used pictures to a useful directory, so that regular cleaning is useless. Directory will do.

 代码如下 复制代码
//转移临时文件夹中的图片
    $imgssss = preg_match_all("/linshi/[^s'"]+.jpg|png|gif|jpeg{1}/ui",$content,$imgss);
    $i=1;
    foreach($imgss as $aimg){
    if(is_string($aimg)){
    if (file_exists($aimg)){
    $newdir = "upload/".date("ymdhis")."/";
    if (!file_exists($newdir)){
    mkdir($newdir,0755,true);
    }
    $newname = $newdir.date("ymdhis").$i.".".pathinfo($aimg,PATHINFO_EXTENSION);
    rename($aimg,$newname);
    $content = str_replace($aimg,$newname, $content);
    }
    $i++;
    }elseif(is_array($aimg)){
    foreach($aimg as $imga){
    if (file_exists($imga)){
    $newdir = "upload/".date("ymdhis")."/";
    if (!file_exists($newdir)){
    mkdir($newdir,0755,true);
    }
    $newname = $newdir.date("ymdhis").$i.".".pathinfo($imga,PATHINFO_EXTENSION);
    rename($imga,$newname);
    $content = str_replace($imga,$newname, $content);
    }
    $i++;
    }
    }
    }

If you just move a single image it is easier:

The code is as follows
 代码如下 复制代码

$newdir = "upload/".date("ymdhis")."/";
if (!file_exists($newdir)){
mkdir($newdir,0755,true);
}
$newname = $newdir."s_".date("ymdhis").".".pathinfo($upimgurl,PATHINFO_EXTENSION);
rename($upimgurl,$newname);

Copy code
$newdir = "upload/".date("ymdhis")."/";
if (!file_exists($newdir)){
mkdir($newdir,0755,true);
}
$newname = $newdir."s_".date("ymdhis").".".pathinfo($upimgurl,PATHINFO_EXTENSION);
rename($upimgurl,$newname);

http://www.bkjia.com/PHPjc/631500.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/631500.html
TechArticle
This article will list some commonly used PHP regular rules to extract images in articles, and replace and move the image directory code , I hope this article will be helpful to everyone. When uploading a picture, upload...
Statement:
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