Collection of string functions in php

Source: Internet
Author: User

Truncates GB2312 Chinese strings.

The code is as follows: Copy code

<? Php

// Truncate a Chinese string

Function mysubstr ($ str, $ start, $ len ){

$ Tmpstr = "";

$ Strlen = $ start + $ len;

For ($ I = 0; $ I <$ strlen; $ I ++ ){

If (ord (substr ($ str, $ I, 1)> 0xa0 ){

$ Tmpstr. = substr ($ str, $ I, 2 );

$ I ++;

} Else

$ Tmpstr. = substr ($ str, $ I, 1 );

}

Return $ tmpstr;

}

?>

2. Truncate UTF-8 encoded multi-byte strings

The code is as follows: Copy code

<? Php

// Truncate the utf8 string

Function utf8Substr ($ str, $ from, $ len)

{

Return preg_replace ('# ^ (? : [X00-x7F] | [xC0-xFF] [x80-xBF] +) {0, '. $ from .'}'.

'((? : [X00-x7F] | [xC0-xFF] [x80-xBF] +) {0, '. $ len.'}). * # s ',

'$ 1', $ str );

}

?>

3. Chinese character truncation functions supported by UTF-8 and GB2312 <? Php

/*

Chinese character truncation functions supported by Utf-8 and gb2312

Cut_str (string, truncation length, start length, encoding );

The default encoding format is UTF-8.

The default start length is 0.

*/

The code is as follows: Copy code

Function cut_str ($ string, $ sublen, $ start = 0, $ code = 'utf-8 ')

{

If ($ code = 'utf-8 ')

{

$ Pa = "/[x01-x7f] | [xc2-xdf] [x80-xbf] | xe0 [xa0-xbf] [x80-xbf] | [xe1-xef] [x80-xbf] [x80-xbf] | xf0 [x90-xbf] [x80-xbf]] [x80-xbf] | [xf1-xf7] [x80-xbf] [x80-xbf] [x80-xbf]/";

Preg_match_all ($ pa, $ string, $ t_string );

 

If (count ($ t_string [0])-$ start> $ sublen) return join ('', array_slice ($ t_string [0], $ start, $ sublen )). "... ";

Return join ('', array_slice ($ t_string [0], $ start, $ sublen ));

}

Else

{

$ Start = $ start * 2;

$ Sublen = $ sublen * 2;

$ Strlen = strlen ($ string );

$ Tmpstr = '';

 

For ($ I = 0; $ I <$ strlen; $ I ++)

{

If ($ I >=$ start & $ I <($ start + $ sublen ))

{

If (ord (substr ($ string, $ I, 1)> 129)

{

$ Tmpstr. = substr ($ string, $ I, 2 );

}

Else

{

$ Tmpstr. = substr ($ string, $ I, 1 );

}

}

If (ord (substr ($ string, $ I, 1)> 129) $ I ++;

}

If (strlen ($ tmpstr) <$ strlen) $ tmpstr. = "...";

Return $ tmpstr;

}

}

 

$ Str = "the string to be intercepted by abcd ";

Echo cut_str ($ str, 8, 0, 'gb2312 ');

?>

4. BugFree character truncation function <? Php

The code is as follows: Copy code

/**

* @ Package BugFree

* @ Version $ Id: FunctionsMain. inc. php, v 1.32 11:38:37 wwccss Exp $

*

*

* Return part of a string (Enhance the function substr ())

*

* @ Author Chunsheng Wang <wwccss@263.net>

* @ Param string $ String the string to cut.

* @ Param int $ Length the length of returned string.

* @ Param booble $ Append whether append "...": false | true

* @ Return string the cutted string.

*/

Function sysSubStr ($ String, $ Length, $ Append = false)

{

If (strlen ($ String) <= $ Length)

{

Return $ String;

}

Else

{

$ I = 0;

While ($ I <$ Length)

{

$ StringTMP = substr ($ String, $ I, 1 );

If (ord ($ StringTMP) >=224)

{

$ StringTMP = substr ($ String, $ I, 3 );

$ I = $ I + 3;

}

Elseif (ord ($ StringTMP) >=192)

{

$ StringTMP = substr ($ String, $ I, 2 );

$ I = $ I + 2;

}

Else

{

$ I = $ I + 1;

}

$ StringLast [] = $ StringTMP;

}

$ StringLast = implode ("", $ StringLast );

If ($ Append)

{

$ StringLast. = "...";

}

Return $ StringLast;

}

}

 

$ String = "111cn.net -- simple, brilliant, and general ";

$ Length = "18 ";

$ Append = false;

Echo sysSubStr ($ String, $ Length, $ Append );

?>

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.