Php string truncation function (Chinese string) (1/2) _ PHP Tutorial

Source: Internet
Author: User
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'

'; &lt;Br/&gt; echo html_substr ($ str, 5 ). php_eol; &lt;br/&gt; echo html_substr ($ str, 5, 1 ); &lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt; 1 2 &lt;/p&gt; &lt;p align = "left"&gt; &lt;p style = "display: none; "&gt; 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 ,... &lt;/p&gt; &lt;p class = "art_confoot"&gt;

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.