Php truncates string functions (Chinese Strings) (12 ). Php intercepts string functions (Chinese strings). This is a php string function. it supports Chinese strings. it can intercept html, Chinese and Western text, and other mixed content, php intercepts string functions (Chinese strings). This is a php string function. it supports Chinese strings. it can intercept html, Chinese and Western text, and other mixed content, in addition, the html tag is not included in the character truncation. if the html tag is not closed, the program will automatically filter redundant tags.
Php Tutorial intercepts string functions(Chinese string)
This is a php string truncation function. it supports Chinese strings. it can intercept html and Chinese and Western text, and separate html tags from characters, if the html tag is not closed, the program automatically filters additional tags.
*/
Function mysubstr ($ str, $ length ){
$ Tagcnt = 0;
$ Charcnt = 0;
$ Tag = '';
$ Maxlen = strlen ($ str );
$ Resultstr = '';
$ Tagstack = array ();
For ($ I = 0; $ I <$ length; $ I ++ ){
If ($ str [$ I] = '<'){
$ Resultstr. = $ str [$ I];
For ($ j = $ I; $ str [$ j]! = '>'; $ J ++, $ length ++ ){
$ Tag. = $ str [$ j];
}
$ Tagcnt ++;
$ Length ++;
$ Tag. = '> ';
// If it is the start tag, it is the inbound stack. if it is the end tag corresponding to it, it is the outbound stack.
If (preg_match ('/<([^/] +)?> /I ', $ tag, $ r )){
Echo 'Inbound stack: ', htmlspecialchars ($ r [1]),'
';
Array_push ($ tagstack, $ r [1]);
}
Elseif (preg_match ('/'. $ tagstack [count ($ tagstack)-1]. '/', $ tag )){
Echo 'output stack: ', htmlspecialchars ($ tagstack [count ($ tagstack)-1]),'
';
Array_pop ($ tagstack );
}
$ Tag = '';
Continue;
}
$ Charcnt ++;
$ Resultstr. = $ str [$ I];
}
Echo'
The final result is :';
// Stack is empty and direct return
If (empty ($ tagstack )){
Return $ resultstr;
}
// Otherwise, remove the start tag without the end tag.
Else {
While (! Empty ($ tagstack )){
$ Tag = array_pop ($ tagstack );
$ Index = strrpos ($ resultstr, $ tag );
For ($ I = $ index-1; $ resultstr [$ I]! = '>'; $ I ++ ){
$ Resultstr [$ I] = '';
}
$ Resultstr [$ I ++] = '';
}
Return $ resultstr;
}
}
$ Sttime = microtime (true );
$ Stmem = memory_get_usage ();
$ Str = "a1B2
C3D4E5
F6G7h8 ";
Echo 'processing result:
', Htmlspecialchars (mysubstr ($ str, 18 )),'
';
Echo "memory usage:", (memory_get_usage ()-$ stmem ),'
';
Echo "microtime:", (microtime (true)-$ sttime ),'
';
// Method 2
/
**
* Function name: html_substr
* The function truncates a string of the specified length from an html string. the html tag is not included.
* Parameters
* $ Str string to be intercepted
* $ Len length to be intercepted
* $ Mode: the processing method of the unmatched tag is 0 (default), and 1 is filled.
* Returns the intercepted string.
* Description
* Multi-byte characters are not considered. only bytes are counted.
* Independent tags are not considered
**/
Function html_substr ($ str, $ len, $ mode = 0 ){
$ Ar = preg_split ('/( | <[^>] *>)/S', $ str,-1, preg_split_delim_capture );
Foreach ($ ar as $ k => $ v ){
If ($ v {0 }! = '<'){
$ Len = $ len-strlen ($ v );
If ($ len <0) $ ar [$ k] = substr ($ v, 0, $ len );
} Else $ ar [$ k] = strtolower ($ v );
If ($ len <= 0) break;
}
$ Ar = array_slice ($ ar, 0, $ k + 1 );
$ Len = count ($ ar );
Foreach ($ ar as $ k => $ v ){
If ($ v {0 }==' <'& $ v [1]! = '/'){
$ Ch = str_replace ('<',' For ($ I = $ k + 1; $ I <$ len & $ ar [$ I]! = $ Ch; $ I ++ );
If ($ I = $ len)
If ($ mode)
$ Ar [$ len] = $ ch. $ ar [$ len];
Else
$ Ar [$ k] = '';
}
}
Return join ('', $ ar );
}
$ Str = "123Abc456Def789 ";
Echo'
'; <Br/> echo html_substr ($ str, 5 ). php_eol; <br/> echo html_substr ($ str, 5, 1 ); </p> </blockquote> <p> 1 2 </p> <p align = "left"> <p style = "display: none; "> http://www.bkjia.com/PHPjc/632042.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/632042.htmlTechArticlephp truncate string function (Chinese string) this is a php truncate string function Oh, this is a support for Chinese strings Oh, it can intercept html and Chinese and Western, and other mixed content ,... </p> <p class = "art_confoot">