Automatically generate the article summary code [PHP version]. Implementation content: truncate a piece of text containing HTML code, but there will be no issue that the congestion mark is not closed. Note: This version is for PHP and is used on the server. if you need to implement the following content: truncate a piece of text containing HTML code, but there will be no issue that the congestion mark is not closed.
Note: This is a PHP version used on the server. if you need a client version, read the next article.
When writing a program such as BLOG, we often need to display the first part of the article, but we are afraid of improper truncation and destruction of closed labels to cause damage to the entire document structure, using my function can solve this problem without high requirements.
You should consider whether this function is used on the server or on the client. Because I think this function may run on a rather expensive machine, it can be placed on the client if the security requirements are not high.
It is best to put this abstract in a separate field in the data table, so that the corresponding database query is also optimized. It is cost-effective to sacrifice a little space for a lot of time.
Let's talk about security issues, mainly about content security. If the client intends to change the normal summary information, it is generally the power of the BLOG master, then he makes the inconsistency between the abstract and the original is his own thing. Security other than content can be solved on the server. Therefore, we recommend that you use this function on the client.
Core code
The code is as follows:
// PHP 4.3 or above needed
Define ("BRIEF_LENGTH", 800); // Word amount of the Briefing of an Article
Function Generate_Brief ($ text ){
Global $ Briefing_Length;
If (strlen ($ text) <= BRIEF_LENGTH) return $ text;
$ Foremost = 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) [^>] * (> ?) /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 is Closing Part
$ Matches [$ I] [2]: Element Name.
$ Matches [$ I] [3]: Right> of 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 have any problems (the above functions do not support the multi-byte character set well), try the following!
The code is 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 is Closing Part
$ Matche [2]: Element Name.
$ Matche [3]: Right> of 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;
};
Bytes. Note: this is for PHP and is used on the server. if you need...