Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of garbled codes in Php mbsubstr

Shulou Source: shulou.com Published: 2022-06-03 17:32:53 09月11日 Update

This article mainly explains "how to solve the problem of Chinese garbled code in Php mbsubstr". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to solve the problem of Chinese garbled code in Php mbsubstr".

1. Use the mb_substr () interception of the mbstring extension library.

2. Solve the garbled code problem through the custom "function sub_str () {...}" method.

You can use the function mb_substr () / mb_strcut (). The use of mb_substr () / mb_strcut () is similar to substr (), except that one more parameter is added at the end of mb_substr () / mb_strcut to set the encoding of the string.

But most servers do not open php_mbstring.dll, so you need to open php_mbstring.dll in php.ini.

Echo mb_substr ("php Chinese character encode", 0jue 4, "utf-8")

If the last encoding parameter is not specified, it will be three bytes into one Chinese, which is the characteristic of utf-8 coding. If you add the utf-8 character set description, it is intercepted in a word unit.

When using, you should pay attention to the encoding of the php file and the coding of the web page display. To use this mb_substr method, you need to know the encoding of the string in advance, and if you don't know the encoding, you need to judge. The mbstring library also provides mb_check_encoding to verify the string encoding, but it is not perfect.

PHP comes with several string intercepting functions, among which substr and mb_substr are commonly used. When the former deals with Chinese, GBK is 2 length units and UTF is 3 length units. After the latter is specified, one Chinese language is 1 length unit.

Substr sometimes truncates 3 or half of Chinese characters and displays garbled codes. Relatively speaking, mb_substr is more suitable for us to use. But sometimes mb_substr doesn't seem to work so well. For example, I want to display a brief message of a small picture. Five Chinese words are just right. If more than five, intercept the first four and add "…" It is no problem to deal with Chinese in this way, but it is too short to intercept English or numbers.

Thank you for your reading, the above is the content of "how to solve the problem of Chinese garbled code in Php mbsubstr". After the study of this article, I believe you have a deeper understanding of how to solve the problem of Chinese garbled code in Php mbsubstr. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Chinese coding problem garbled character unit string that is length utf-8 processing learning content function parameter method similarity three information just Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Docker vpn Apple Shulou Tech Info