PHP mb_substr () function intercept Chinese string application example _php tutorial

Source: Internet
Author: User

PHP mb_substr () function intercept Chinese string application example


The substr () function is used to intercept strings, but there is a problem with Chinese characters, and mb_substr () and mb_strcut are the two functions, so let's introduce their specific usage.

The substr () function is used to intercept strings, but there is a problem with Chinese characters, and mb_substr () and Mb_strcut functions can be similar to substr (), except that at the end of the function you add more than one parameter to set the encoding of the string. Using these two functions requires the Php_mbstring.dll to be opened in the php.ini.

  

Header ("content-type:text/html; Charset=utf-8 ");

$string = "Hello i good everyone";

echo strlen ($string). '
';

Echo mb_substr ($string, 0,4, ' utf-8 '). ' ...
';

Echo mb_strcut ($string, 0,4, ' utf-8 '). ' ...';

?>

Results of the output:

21st

Hello I'm good ...

You...

As can be seen from the above example, Mb_substr is to divide characters by word, while mb_strcut divides characters by bytes, but does not produce a half-character phenomenon.

http://www.bkjia.com/PHPjc/852829.html www.bkjia.com true http://www.bkjia.com/PHPjc/852829.html techarticle The php mb_substr () function intercepts the Chinese string application example substr () function is used to intercept strings, but for Chinese characters there is a problem, and mb_substr () and mb_strcut are two functions ...

  • 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.