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)译

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version
SublimeText3 Linux latest version

Zend Studio 13.0.1
Powerful PHP integrated development environment
