Code for automatically generating article digests [PHP version]_php tips

Source: Internet
Author: User
Implementation content: Truncate a section of text containing HTML code, but there is no containment tag does not have a closed problem.
Description: This is the PHP version, used on the server side, if you need a client version, please read the next
We are writing a blog such a program often need to show the previous part of the article, but also afraid of improper truncation to destroy the closed tag to cause the entire document structure damage, using my function can be in a low demand to solve the problem.
You should consider whether this function is applied on the server or on the client side. Because I consider this function may run up to compare the cost of the machine, so the security requirements are not high in the case can be placed on the client.
The best data table in a single field to put this summary, so that the corresponding database query optimization. Sacrificing a little space for a lot of time is worth it.
To talk about security, mainly content security. If the client intends to change the normal summary information, it is generally the owner of the blog has this power, then he made the summary and the original consistency of the destruction is his own business. Security outside the content can be resolved on the server side. So it is recommended that you use this function on the client.
Core code

Copy Code code as follows:

PHP 4.3 or above needed
Define ("Brief_length", 800); Word amount of the Briefing of Article
function Generate_brief ($text) {
Global $Briefing _length;
if (strlen ($text) <= brief_length) return $text;
$Foremost = substr ($text, 0, brief_length);
$re = "/< (\/?) (p| div| F12 h2| h3| h4| h5| h6| address| pre| table|tr| td|th| Input| Select| textarea| Object| a| ul| ol| li| base| meta| link|hr|br| param| Img| area| Input| SPAN) [^>]*] /I ";
$Single = "/base| meta| link|hr|br| param| Img| area| Input/i ";
$Stack = Array (); $posStack = Array ();
Preg_match_all ($re, $Foremost, $matches, Preg_set_order | Preg_offset_capture);
/* [child-matching Specification]:
$matches [$i][1]: A "/" charactor indicating whether current "<...>" friction are Closing part
$matches [$i][2]: Element Name.
$matches [$i][3]: Right > A "<...>" friction * *
for ($i = 0; $i < count ($matches); $i + +) {
if ($matches [$i][1][0] = = "") {
$Elem = $matches [$i][2][0];
if (Preg_match ($Single, $Elem) && $matches [$i][3][0]!= "") {
Continue
}
Array_push ($Stack, Strtoupper ($matches [$i][2][0]));
Array_push ($posStack, $matches [$i][2][1]);
if ($matches [$i][3][0] = = "") break;
}else{
$StackTop = $Stack [Count ($Stack)-1];
$End = Strtoupper ($matches [$i][2][0]);
if (strcasecmp ($StackTop, $End) ==0) {
Array_pop ($Stack);
Array_pop ($posStack);
if ($matches [$i][3][0] = = "") {
$Foremost = $Foremost. " > ";
}
}
}
}
$cutpos = Array_shift ($posStack)-1;
$Foremost = substr ($Foremost, 0, $cutpos);
return $Foremost;
};
If you encounter a problem (find that the above function does not support multibyte character sets) don't bother to try this one below!

Copy Code code as follows:

function Generate_brief ($text) {
Global $Briefing _length;
Mb_regex_encoding ("UTF-8");
if (Mb_strlen ($text) <= brief_length) return $text;
$Foremost = mb_substr ($text, 0, brief_length);
$re = "< (\/?) (p| div| h1| h2| h3| h4| h5| h6| address| pre| table|tr| td|th| Input| Select| textarea| Object| a| ul| ol| li| base| meta| link|hr|br| param| Img| area| Input| SPAN) [^>]*];
$Single = "/base| meta| link|hr|br| param| Img| area| Input|br/i ";
$Stack = Array (); $posStack = Array ();
Mb_ereg_search_init ($Foremost, $re, ' I ');
while ($pos = Mb_ereg_search_pos ()) {
$match = Mb_ereg_search_getregs ();
/* [child-matching formulation]:
$matche [1]: A "/" charactor indicating whether current "<...>" friction are Closing part
$matche [2]: Element Name.
$matche [3]: Right > A "<...>" friction
*/
if ($match [1]== "") {
$Elem = $match [2];
if (Mb_eregi ($Single, $Elem) && $match [3]!= "") {
Continue
}
Array_push ($Stack, Mb_strtoupper ($Elem));
Array_push ($posStack, $pos [0]);
}else{
$StackTop = $Stack [Count ($Stack)-1];
$End = Mb_strtoupper ($match [2]);
if (strcasecmp ($StackTop, $End) ==0) {
Array_pop ($Stack);
Array_pop ($posStack);
if ($match [3] = = "") {
$Foremost = $Foremost. " > ";
}
}
}
}
$cutpos = Array_shift ($posStack)-1;
$Foremost = mb_substr ($Foremost, 0, $cutpos, "UTF-8");
return $Foremost;
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.