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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.
