How to solve the problem of garbled characters intercepted by PHPsubstr [utf8 and gb2312] _ PHP Tutorial

Source: Internet
Author: User
How to solve the problem of garbled characters in the string intercepted by PHPsubstr [utf8 and gb2312]. Substr --- obtain part of the string syntax: stringsubstr (stringstring, intstart [, intlength]) description: substr () returns a part of the string, specified by the start and length parameters. Substr --- obtain some strings
Syntax: string substr (string, int start [, int length])
Note:
Substr () returns a string that is specified by the start and length parameters.
If start is a positive number, the returned string starts with the start character of the string.
Example:

The code is as follows:


$ Rest = substr ("abcdef", 1); // returns "bcdef"
$ Rest = substr ("abcdef", 1, 3); // returns "bcd"
?>


If start is a negative number, the returned string starts with the start character at the end of the string.
Example:

The code is as follows:


$ Rest = substr ("abcdef",-1); // returns "f"
$ Rest = substr ("abcdef",-2); // returns "ef"
$ Rest = substr ("abcdef",-3, 1); // returns "d"
?>


If the length parameter is given and it is a positive number, the returned string will return length characters from start.
If the length parameter is given and it is a negative number, the returned string ends with the nth length character ending with the string.
Example:

The code is as follows:


$ Rest = substr ("abcdef", 1,-1); // returns "bcde"
?>


Dongyue, 11:10:41
Substr --- obtain some strings
Syntax: string substr (string, int start [, int length])
Note:
Substr () returns a string that is specified by the start and length parameters.
If start is a positive number, the returned string starts with the start character of the string.
Example:

The code is as follows:


$ Rest = substr ("abcdef", 1); // returns "bcdef"
$ Rest = substr ("abcdef", 1, 3); // returns "bcd"
?>


If start is a negative number, the returned string starts with the start character at the end of the string.
Example:

The code is as follows:


$ Rest = substr ("abcdef",-1); // returns "f"
$ Rest = substr ("abcdef",-2); // returns "ef"
$ Rest = substr ("abcdef",-3, 1); // returns "d"
?>


If the length parameter is given and it is a positive number, the returned string will return length characters from start.
If the length parameter is given and it is a negative number, the returned string ends with the nth length character ending with the string.
Example:

The code is as follows:


$ Rest = substr ("abcdef", 1,-1); // returns "bcde"
?>


Chinese character truncation functions supported by Utf-8 and gb2312

The code is as follows:


// Truncate a Chinese string
/*
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.
*/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 = "a good website for my feet ";
Echo cut_str ($ str, 8, 5, 'gb2312 ');

Http://www.bkjia.com/PHPjc/324655.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/324655.htmlTechArticlesubstr --- get part of the string syntax: string substr (string, int start [, int length]) description: substr () returns a part of the string, specified by the start and length parameters...

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.