php interception of Chinese string code

  • 2020-12-09 00:46:09
  • OfStack

Example of GBK encoding interception


$str = ' Who am I ';  //gbk Encoded string 
echo mb_substr($str, 0, 1, 'gbk'); // The output   I 

The mb_substr method takes one more argument than substr to specify the string encoding.

Example of utf-8 encoding interception

[code]
$str = 'Who am I abc '; //utf-8 encoded string
echo mb_substr($str, 0, 2, 'utf-8'); Output me a
[/code

There is no problem with mixing China and Britain.

Helpful hints

Pay attention to the code of the php file and the code of the web page.

Using the mb_substr method requires you to know the encoding of the string beforehand, and if you don't know the encoding, you need to determine. The mbstring library also provides mb_check_encoding to verify the encoding of the string, but it is not perfect.


Related articles: