Array_diff ($arr 1, $arr 2)
One of the PHP array functions used to compute the difference set of an array.
Regular matching HTML picture label
Picture delete operation added with Sinaeditor
One of the ways to publish an article this evening in the Sina editor.
This function was used
Problem Description:
There are a number of pictures in the article. Automatically uploaded to the site's image catalogue during the addition of the article
If the picture is deleted during the modification of the article. So although in the code (already stored in the database);
The label for the data has been deleted. A label like . But the picture file still exists in
On the website. This time it needs some processing.
Treatment methods:
First: Get the original article content from the database
get the file name of the picture from inside
With a regular
Methods are 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 <count ($temp); $i + +)
{
$imgs [$i] = PathInfo ($temp [$i]);
$imgs [$i] = $imgs [$i] [' basename '];
}
return $imgs;
}
Else
{
return false;
}
}
For regular explanations, first match the HTTP four letters and then match the number of Non-numeric characters. Match numeric characters to
One less, matching point (.) One that matches a JPG or GIF or PNG end to look up from the $congtent. The result is deposited in $temp.
Save the pictures in the original data in the database in an array. Named $oldimgs
This place I think should be improved, after the deposit is printed out is a two-dimensional array. It's a bit of a bother.
Note: My picture name is similar to the one named: "201111291322589013.jpg"
Step Two:
Find all the pictures from the content submitted by the user as above. Get array two named $newimgs
The method for arr1 and arr2 difference sets is as follows
-that is, if the picture in the original data does not exist in the user's newly submitted content. Then the picture 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 <count ($diff); $i + +)
{
$this->delimg ($diff [$i],article_img_dir);
}
}
}
the way to delete a picture is as simple as this.
The
code is as follows:
Public Function delimg ($imgname, $dir)
{
@unlink ($dir. '/'. $imgname);
return true;
}
So my purpose is achieved. When the user edits an article with a picture. If you delete the picture, the corresponding picture will also be deleted from the site.
The method of getting the name of the picture in the article can also be applied to the process of deleting the article.
$dir in the method of deleting a picture can be Realpath (__file__) plus various "./" "... /"To give a catalogue of the pictures in relation to the site
For getting the path in the HTML the regular writing here is not very good. To be studied. A regular book was recently found. It's good.