The phpmb_substr function intercepts Chinese characters.

Source: Internet
Author: User
The phpmb_substr function intercepts Chinese characters. The php mb_substr function intercepts the first two and the last two characters of a variable. the variable content is a Chinese character, but only the first two characters can be output, the last two characters cannot be output. Does the mb_substr function not support truncation from the end? Also, give pointers.
$ Ip = "Foshan Telecom, Guangdong province ";
Echo mb_substr ($ ip, 0, 2, 'utf-8 ');
Echo mb_substr ($ ip,-2, 'utf-8 ');
?>

Current output: Guangdong
Expected Output: Guangdong Telecom


Thank you!


Reply to discussion (solution)

The second sentence should report an error.
String mb_substr (string $ str, int $ start [, int $ length [, string $ encoding])

The second parameter is length.

The maximum number of characters to use in str.

Mb_internal_encoding ("UTF-8 ");
$ Ip = "Foshan Telecom, Guangdong province ";
Echo mb_substr ($ ip, 0, 2 );
Echo mb_substr ($ ip,-2 );

Dizzy, I saw you in PPC too ..
The problem is quite tangled. $ Chinese characters and spaces must be considered in ip strings. spaces are considered only one length. Therefore, it is not feasible to use strlen to determine the character length and then intercept the character.

 

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.