PHP Chinese no garbled interception

Source: Internet
Author: User

Uploading files anyway bored to the PHP without garbled intercept write it.

Parameter description

GBK character encoding, store Chinese characters to 2 bytes

uft-8 character encoding, store Chinese characters to 3 bytes

0xa0 half Chinese characters

Ord ()- Returns the ASCII code value of the character

chr () - Returns the specified character

I also see some functions that intercept Chinese on the internet but I think they're flawed. If the character set of Utf-8 GBK cannot be distinguished then I wrote a

But I found that I wrote the function is actually using MB series function, I also thin

Header("content-type:text/html; CharSet = GBK "); functionCHINESESUBSTR ($str,$star,$len,$char _set= ' Uft8 '){        $tmpstr=NULL; $char _set= = ' GBK '?$char _set= 2:$char _set= 3;//To intercept different lengths depending on the character set        $strlen=$star+$len;  for($i= 0;$i<$strlen;$i++ ){            if(Ord(substr($str,$i, 1)) >0xa0){                 $tmpstr.=substr($str,$i,$char _set); $i++; }Else{                 $tmpstr.=substr($str,$i, 1); }        }        return $tmpstr; }//English (special symbol---some things do not distinguish between Chinese and English #%*, etc.): 3 numbers: 1 letters (special Symbols): 1EchoChinesesubstr (' Play QQQ extra people hear ', 0,5, ' GBK ');

So, honestly, write a MB-series function.

functionCHINESESUBSTR ($str,$star=0,$len,$encoding= ' UTF8 '){    $strlen=strlen($str); if($strlen<$len){        return $str; }Else{        $re _turn= Mb_substr ($str,$star,$len,$encoding).CHR(0). ' ...‘; return $re _turn; } }$str= "This news or the title of the article is very long, need to show only the previous words, followed by ... To replace the ";EchoCHINESESUBSTR ($str, 0,5);

End of Knowledge point

When the Chinese characters are truncated, according to the coding rules he always vlasov the other words in the back to be interpreted as Chinese characters,

This is the reason for garbled characters. A value of 0x81 to 0xFF and 0x00 combinations is always displayed as "empty" according to this feature,

A Chr (0) is appended to the result of the substr to prevent garbled characters.

chr (0) Related knowledge :
Null is nothing, and Chr (0) has a value of 0. 16 binary is 0x00, which means binary is 00000000
Although Chr (0) will not show anything, but he is a character.

PHP Chinese no garbled interception

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.