Use of image processing in php articles. Array_diff ($ arr1, $ arr2) is one of the php array functions used to calculate the difference set of the array. this is one of the operations used to delete images added by sinaeditor for regular expression matching html image tags. sina
Array_diff ($ arr1, $ arr2)
One of the php array functions is used to calculate the array's difference set.
Regular Expression Matching html image tags
Delete images added with sinaeditor
Usage 1: This evening, when I published an article in The Sina editor.
This function is used.
Problem description:
There are several images in the article. The images will be automatically uploaded to the image directory of the website during the process of adding the article.
If you delete an image when you modify the document, the image is stored in the database in the code );
Tags of data have been deleted. tags similar to this are deleted. However, the image file still exists in
On the website. some processing is required at this time.
Solution:
First, get the original article content from the database.
Get the image file name from it
The regular expression is used.
The method is as follows:
The code is as follows:
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;
}
}
To explain the regular expression, match the http four letters first and then match the non-numeric characters. match the numeric characters
One fewer match point (.). The match ends with jpg, gif, or png and searches for it from $ congtent. The result is saved to $ temp.
Save the images in the original data in the database in the array. name it $ oldimgs
In this case, I think it should be improved to save it and print it out as a two-dimensional array.
Note: my image name is similar to this: "201111291322589013.jpg"
Step 2:
Find all the image methods from the content submitted by the user as above. get array 2 named $ newimgs
The following method is used to evaluate the difference set between arr1 and arr2:
-- If the image in the original data does not exist in the content submitted by the user, the image will be deleted.
The code is as follows:
$ 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 );
}
}
}
The method for deleting an image is as follows.
The code is as follows:
Public function delimg ($ imgname, $ dir)
{
@ Unlink ($ dir. '/'. $ imgname );
Return true;
}
In this way, my goal is achieved. when the user edits an article with an image, if the image is deleted, the corresponding image will also be deleted from the website.
The method for getting the image name in the article can also be applied to the process of deleting the article.
In the method of deleting an image, you can use realpath (_ FILE _) to add various "./" ../"to show the Directory of the image relative to the website.
It is not very good to get the path in html. it is still to be studied. Recently, I found a regular book. it is very good.
One of the php array functions of http://www.bkjia.com/PHPjc/371921.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/371921.htmlTechArticlearray_diff ($ arr1, $ arr2) is used to calculate the difference set of arrays. one of the operations for deleting images added with sinaeditor for regular expression matching html image tags. Sina is used this evening...