Php Chinese string truncation method example _ php skills

Source: Internet
Author: User
This article mainly introduces the php Chinese string truncation method example, analyzes and compares common string truncation functions, and provides a complete example to solve the PHP Chinese string truncation problem, for more information about how to intercept Chinese strings in php, see the examples in this article. Share it with you for your reference. The specific method is analyzed as follows:

Using the PHP function substr to intercept Chinese characters may cause garbled characters, mainly because substr may generate a Chinese character "sawing" into two halves.

The solution is as follows:

1. use mb_substr of the mbstring Extension Library to intercept the database without garbled characters.

2. write the truncation function by yourself, but it is not as efficient as using the mbstring Extension Library.

3. if you only want to output the intercepted string, use the following method: substr ($ str, 0, 30). chr (0 ).

The substr () function can split text. However, if the text to be split contains Chinese characters, you can use the mb_substr ()/mb_strcut function, mb_substr () the usage of/mb_strcut is similar to that of substr (), but an additional parameter must be added at the end of mb_substr ()/mb_strcut to set the character string encoding. However, php_mbstring.dll is not enabled on the server, in php. ini opens php_mbstring.dll.

Here are two examples:

① Mb_substr example

<? Phpecho mb_substr ('in this way, my strings will not contain garbled characters such as ^_^', 0, 7, and 'utf-8'); // output: So my words are like this?>

② Mb_strcut example

<? Phpecho mb_strcut ('so that my string will not contain garbled characters such as ^_^', 0, 7, 'utf-8'); // The output is as follows:

In the preceding example, we can see that mb_substr is used to split characters by words, while mb_strcut is used to split characters by bytes, but it does not produce half a character.

PHP:

<? Php // This function completes string acquisition function substr_CN ($ str, $ mylen) {$ len = strlen ($ str); $ content = ''; $ count = 0; for ($ I = 0; $ I <$ len; $ I ++) {if (ord (substr ($ str, $ I, 1)> (127) {$ content. = substr ($ str, $ I, 2); $ I ++;} else {$ content. = substr ($ str, $ I, 1);} if (++ $ count = $ mylen) {break ;}} echo $ content ;} $ str = "34 People's Republic of China 56"; substr_CN ($ str, 3); // output in 34?>

I hope this article will help you with PHP programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.