PHP intercepts html code and considers the html tag closure issue/* generates the abstract * @ param (string) $ body * @ param (int) $ size * summary length * @ param (int) $ format * input format id */functionblog_summary ($ body, $ si...
PHP intercepts html code and considers html tag closure.
/* Generate a summary
* @ Param (string) $ body
* Body
* @ Param (int) $ size
* Digest length
* @ Param (int) $ format
* Input format id
*/
Function blog_summary ($ body, $ size, $ format = NULL ){
$ _ Size = mb_strlen ($ body, "utf-8 ');
If ($ _ size <= $ size) return $ body;
// PHP filters are included in the input format
/*
If (! Isset ($ format) & filter_is_php ($ format )){
Return $ body;
}
*/
$ Strlen_var = strlen ($ body );
// Does not contain html tags
If (strpos ($ body, '<') = false ){
Return mb_substr ($ body, 0, $ size );
}
// Contains the truncation mark. priority is given.
If ($ e = strpos ($ body ,' ')){
Return mb_substr ($ body, 0, $ e );
}
// Html code mark
$ Html_tag = 0;
// Digest string
$ Summary_string = '';
/**
* The array is used as the html tag within the record summary range.
* Start and end are stored in the left and right keys respectively.
* If the string is:
AAssume that p is not closed.
* Array: array ('left' => array ('h3 ', 'P',' B '), 'right' =>' B ', 'h3 ');
* Only add html tags, */
$ Html_array = array ('left' => array (), 'right' => array ());
For ($ I = 0; $ I <$ strlen_var; ++ $ I ){
If (! $ Size ){
Break;
}
$ Current_var = substr ($ body, $ I, 1 );
If ($ current_var = '<'){
// Html code starts
$ Html_tag = 1;
$ Html_array_str = '';
} Else if ($ html_tag = 1 ){
// The end of a piece of html code
If ($ current_var = '> '){
/**
* Remove spaces at the beginning and end, as shown in figure
.
*/
$ Html_array_str = trim ($ html_array_str );
/**
* Determines whether the last character is/. If yes, the tag is closed and not recorded.
*/
If (substr ($ html_array_str,-1 )! = '/'){
// Determine whether the first character is/. If yes, it is placed in the right unit.
$ F = substr ($ html_array_str, 0, 1 );
If ($ f = '/'){
// Remove/
$ Html_array ['right'] [] = str_replace ('/', '', $ html_array_str );
} Else if ($ f! = '? '){
// Determine whether it is ?, If yes, PHP code is used. skip this step.
/**
* Determines whether there is a halfwidth space. If yes, it is separated by spaces. The first unit is an html tag.
* For example
*/
If (strpos ($ html_array_str ,'')! = False ){
// Split it into two units. There may be multiple spaces, such:
$ Html_array ['left'] [] = strtolower (current (explode ('', $ html_array_str, 2 )));
} Else {
/**
** If there is no space, the entire string is an html tag, for example:
And so on
* Convert data to lowercase letters in a unified manner.
*/
$ Html_array ['left'] [] = strtolower ($ html_array_str );
}
}
}
// String reset
$ Html_array_str = '';
$ Html_tag = 0;
} Else {
/**
* A string consisting of <> Characters
* Used to extract html tags
*/
$ Html_array_str. = $ current_var;
}
} Else {
// Non-html code is counted
-- $ Size;
}
$ Ord_var_c = ord ($ body {$ I });
Switch (true ){
Case ($ ord_var_c & 0xE0) = 0xC0 ):
// 2 bytes
$ Summary_string. = substr ($ body, $ I, 2 );
$ I + = 1;
Break;
Case ($ ord_var_c & 0xF0) = 0xE0 ):
// 3 bytes
$ Summary_string. = substr ($ body, $ I, 3 );
$ I + = 2;
Break;
Case ($ ord_var_c & 0xF8) = 0xF0 ):
// 4 bytes
$ Summary_string. = substr ($ body, $ I, 4 );
$ I + = 3;
Break;
Case ($ ord_var_c & 0xFC) = 0xF8 ):
// 5 bytes
$ Summary_string. = substr ($ body, $ I, 5 );
$ I + = 4;
Break;
Case ($ ord_var_c & 0xFE) = 0xFC ):
// 6 bytes
$ Summary_string. = substr ($ body, $ I, 6 );
$ I + = 5;
Break;
Default:
// 1 byte
$ Summary_string. = $ current_var;
}
}
If ($ html_array ['left']) {
/**
* Compare the left and right html tags. if not, supplement them.
*/
/**
* The order of left is switched. The order of supplement should be the same as that of html.
* For example, the string to be supplemented is abc.Abc
Abc
* The supplemental order should be:
*/
$ Html_array ['left'] = array_reverse ($ html_array ['left']);
Foreach ($ html_array ['left'] as $ index => $ tag ){
// Determine whether the label appears in right
$ Key = array_search ($ tag, $ html_array ['right']);
If ($ key! = False ){
// Appears. delete the cell from right.
Unset ($ html_array ['right'] [$ key]);
} Else {
// Does not appear and must be supplemented
$ Summary_string. =' ';
}
}
}
Return $ summary_string;
}
-->