Example of function mb_substr in PHP

Source: Internet
Author: User
Tags import database
String Mb_substr (string $str, int $start [, int $length = NULL [, String $encoding = Mb_internal_encoding ()]])-intercept string

(PHP 4 >= 4.0.6, PHP 5)
$str the target string to get the string from (the starting position of the string is 0)
$start, the position of the first character to be used in the $STR
$length, gets the length of the substring (note is not the end position)
$encoding, you can specify character encodings (typically used when working with Chinese characters, and this problem is very much)

<?phpmb_internal_encoding ("UTF-8");    If you change the UTF-8 to encode, the following values for the Chinese string processing will change. Echo mb_internal_encoding (). "";    Gets the character encoding for Iso-8859-1echo mb_substr (' Abcdefghijk ', 0,9). "";    Abcdefghiecho mb_substr (' Abcdefghijk ', 1,5). ";    Bcdefecho Mb_substr (' We are all Chinese ', 0,9). "";    We are all Chinese echo mb_substr (' We are all Chinese ', 0,9, ' gb2312 ').    We are all in echo mb_substr (' We are all Chinese ', 0,9, ' utf-8 ');    We are all Chinese    ?>

Summarize:
1, in the processing of English strings, the function of the fourth parameter ($encoding) can be ignored.
2, in the processing of Chinese strings, it is necessary to be careful, we must consider the coding problem, coding different, Chinese values are different.
3, when the Chinese string import database, it becomes more important.
4, the processing of string or text encoding ability, is a measure of the programmer's skill of a standard.

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.