Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of intercepting Chinese garbled codes by php substr function

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In this issue, the editor will bring you about the php substr function to intercept Chinese garbled code how to solve, the article is rich in content and professional analysis and description for you, after reading this article, I hope you can get something.

The reason why the php substr function intercepts the Chinese characters is that the substr function will treat the Chinese characters of an UTF-8 as 3 bytes. The solution to the garbled code is to use the mb_strlen and mb_substr functions instead.

This article operating environment: Windows7 system, PHP7.1, Dell G3 computer.

What if the php substr function intercepts Chinese garbled codes?

The solution of intercepting Chinese garbled codes by substr in PHP

Substr is often used to intercept strings in PHP, but garbled problems occur when we use it to intercept Chinese characters, such as:

The output is "

Reason analysis: substr function prototype is: string substr (string $string, int $start [, int $length]), where $length represents the returned byte length, while a Chinese occupies two bytes, for a UTF-8 Chinese character, it will be treated as 3 bytes.

In other words, we can make $length 3, so we will successfully return the word "today". In this case, if Chinese and English are mixed, we need to calculate $length carefully.

So is there a better solution? Php gives other functions mb_strlen and mb_substr

The output is: the length is:6,the substr is: today's weather

The prototype of the mb_strlen function is int mb_strlen (string string_input, string encode); encode defaults to UTF-8, which counts the Chinese characters encoded for UTF-8 as a

Note: mb_strlen and mb_substr are not the core functions of PHP. You need to open the extension=php_mbstring.dll item in php.ini before using it.

The above is how to intercept Chinese garbled codes for the php substr function shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report