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

PHP string what is the specific method of dealing with Chinese strings by mbstring

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

Share

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

This article introduces to you what is the specific method of PHP string mbstring to deal with Chinese strings, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Through the study of PHP, you can use this high-level language to create a high-performance website. For beginners, the PHP string mbstring is still relatively unfamiliar, so let's introduce the specific application of PHP string mbstring.

The coexistence of multiple languages means multi-bytes. PHP's built-in string length function strlen cannot correctly handle Chinese strings, it only gets the number of bytes occupied by the string. For the Chinese code of GB2312, the value obtained by strlen is twice the number of Chinese characters, while for the Chinese code of UTF-8, it is 1-3 times the difference.

This problem can be solved by using PHP string mbstring. The use of mb_strlen is similar to strlen, except that it has a second optional parameter to specify the character encoding. For example, to get the string $str length of UTF-8, you can use mb_strlen ($str,'UTF-8'). If the second parameter is omitted, the internal encoding of PHP is used. The internal code can be obtained through the mb_internal_encoding () function, which can be set in two ways:

1. Set mbstring.internal_encoding = UTF-8 in php.ini

two。 Call mb_internal_encoding ("GBK")

In addition to the PHP string mbstring, there are many splicing functions, in which mb_substr splits characters by word, while mb_strcut splits characters by bytes, but none of them produces half a character. Moreover, the effect of function cutting on length is also different. The cutting condition of mb_strcut is less than strlen, and mb_substr is equal to strlen. See the example below.

< ? $str = ‘我是一串比较长的中文-www.jefflei.com’; echo "mb_substr:" . mb_substr($str, 0, 6, ‘utf-8′); echo " "; echo "mb_strcut:" . mb_strcut($str, 0, 6, ‘utf-8′); ?>

The output is as follows:

Mb_substr: I am a series of comparisons

Mb_strcut: I am

It should be noted that the PHP string mbstring is not the core function of PHP. Before using it, you need to make sure that mbstring support is added to the php compilation module:

(1) use-enable-mbstring when compiling

(2) modify / usr/local/lib/php.inc

Default_charset = "zh-cn"

Mbstring.language = zh-cn

Mbstring.internal_encoding = zh-cn

PHP string mbstring class library contains a lot of content, including email processing functions such as mb_ send_ mail.

On the PHP string mbstring to deal with Chinese strings what is the specific method to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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