Home >Backend Development >PHP Tutorial >PHP method to intercept Chinese string (no garbled characters)

PHP method to intercept Chinese string (no garbled characters)

WBOY
WBOYOriginal
2016-07-25 08:55:131011browse
  1. $str = 'Who am I'; //gbk encoded string
  2. echo mb_substr($str, 0, 1, 'gbk'); //Output me
Copy code

The mb_substr method has one more parameter than substr, which is used to specify the string encoding.

2. Example of intercepting Chinese string using UTF-8 encoding.

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

Tips: Mixing Chinese and English is no problem at all.

Attention, pay attention to the encoding of the php file when using it, and the encoding when displaying the web page. To use this mb_substr method, you need to know the encoding of the string in advance. If you don't know the encoding, you can use mb_check_encoding provided by the mbstring library to check the string encoding, but it is not perfect yet.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn