Usage and difference of substr (), mb_substr () and mb_strcut functions

Source: Internet
Author: User

Substr () function

Substr (string, start, length)

String indicates the object to be intercepted, start indicates the starting position, 0 indicates the starting point, positive number indicates the position after the number, and negative number indicates the position starting from the end, but it is still left-to-right cut, length indicates the cut length. negative number indicates how many characters are excluded or ignored. for example:

The code is as follows: Copy code
<? Php
$ Siteurl = 'www .111cn.net ';
Print_r (substr ($ siteurl, 4); exit;

// The return value is 111cn.net, which indicates that the name starts from 4th characters and returns all the subsequent characters.

The code is as follows: Copy code

<? Php
$ Siteurl = 'www .111cn.net ';
Print_r (substr ($ siteurl,-6, 2); exit;

To intercept two-byte Chinese characters. use the PHP mb_substr function or the mb_strcut function, but these two functions depend on the php extension php_mbstring.dll component, so configure your server. copy the php_mbstring.dll file in the php installation directory to the windows/system32 directory of drive C of your Windows 2003.


For example:

The code is as follows: Copy code

<? Php
Echo mb_substr ('in this way, my strings will not contain garbled characters such as ^_^', 0, 7, 'utf-8 ');
?>

Output: in this way, my words

The code is as follows: Copy code

<? Php
Echo mb_strcut ('in this way, my strings will not contain garbled characters such as ^_^', 0, 7, 'utf-8 ');
?>

Output:

From the above example, we can see that mb_substr is a word-based splitting character, while mb_strcut is a byte-based splitting character, but it does not produce a half character ......

The code is as follows: Copy code

<? Php
Echo mb_substr ('flying garden blog feihuayuan );
// Return: Apsara infrastructure management framework

Echo mb_substr ('flying garden blog feihuayuan ', 'utf-8 ');
// Return: feih

Mb_strcut ('flying garden blog feihuayuan ', 'utf-8 ');
Then return: Apsara infrastructure management framework
?>

For another example, we use mb_substr and mb_strcut to split a piece of text:

PLAIN TEXT
CODE:

The code is as follows: Copy code

<? Php
$ Str = 'I am a long string of Chinese characters -';

Echo "mb_substr:". mb_substr ($ str, 0, 6, 'utf-8 ');

Echo "<br> ";

Echo "mb_strcut:". mb_strcut ($ str, 0, 6, 'utf-8 ');
?>

The output result is as follows:

Mb_substr: I am a comparison string
Mb_strcut: I am

Summary

From the above example, we can see that substr only supports single-byte, which is only suitable for English intercept, while the mb_substr function is a dual-font intercept, which can be used in Chinese, while mb_strcut is three bytes.

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.