php笔记之:文章中图片处理的使用_PHP
array_diff($arr1,$arr2)
php数组函数之一,用来计算数组的差集.
正则匹配html图片标签
用sinaeditor添加的图片删除操作
用法之一,今天晚上在用新浪编辑器发表文章的过程中.
使用到了此函数
问题描述:
文章中有图片若干.在增加文章的过程中自动上传到网站的图片目录中
在修改文章的过程中如果对图片进行相关的删除操作.那么虽然在代码中(已经存入数据库);
已经删除了数据的标签.类似于这样的标签.但是图片的文件依旧存在于
网站上.这时候需要一定的处理
处理办法:
首先:从数据库中得到原始的文章内容
从里面得到图片的文件名
用到了正则
方法如下
复制代码 代码如下:
public function getimgsinarticle($content)
{
$temp = array();
$imgs = array();
preg_match_all('/http[^\d]*[\d]+[\.](jpg|gif|png)/',$content,$temp);
$temp = $temp[0];
if(!empty($temp[0]))
{
for($i=0;$i
$imgs[$i] = pathinfo($temp[$i]);
$imgs[$i] = $imgs[$i]['basename'];
}
return $imgs;
}
else
{
return false;
}
}
对正则进行下解释,先匹配http四个字母然后匹配非数字的字符若干个.匹配数字字符至
少一个,匹配点(.)一个,匹配以jpg或gif或png结尾从$congtent中查找.结果存入$temp中.
将数据库中的原始数据中的图片保存在数组中.命名为$oldimgs
这个地方我觉得应该改进下,存入后打印出来是二维数组.用起来有点费事
注:我的图片名称是类似于这个样子命名的:"201111291322589013.jpg"
第二步:
从用户提交过来的内容中找到所有的图片方法如上.得到数组二命名为$newimgs
对arr1和arr2求差集方法如下
--也就是说如果原始数据中的图片不存在于用户新提交的内容中.那么将删除这个图片.
复制代码 代码如下:
$oldimgs = $this->getimgsinarticle($oldarticledata['article_content']);
$newimgs = $this->getimgsinarticle($data['articlecontent']);
//print_r($newimgs);
$newimgs = empty($newimgs)?array():$newimgs;
if($oldimgs!=false)
{
$diff = array_diff($oldimgs,$newimgs);
$diff = array_values($diff);
if(!empty($diff))
{
for($i=0;$i
$this->delimg($diff[$i],ARTICLE_IMG_DIR);
}
}
}
删除图片的方法如下 很简单.
复制代码 代码如下:
public function delimg($imgname,$dir)
{
@unlink($dir.'/'.$imgname);
return true;
}
这样我的目的就达到了.当用户编辑了带有图片的文章.如果删除了图片.那么相应的图片也会从网站上删除
得到文章中的图片名称的方法还可以应用到删除文章的过程中.
在删除图片的方法中的$dir可以用realpath(__FILE__)加上各种"./""../"去给出图片目录相对于网站的目录
对于得到html中的路径这里的正则写的不是很好.有待研究.最近发现一本正则的书.很不错
精通正则表达式第三版 Jeffrey E.F. Friedl著 ,余晟(cheng)译

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
