SUBSTR (), Mb_substr () and mb_strcut function usage and differences _php tutorial

Source: Internet
Author: User
in PHP substr (), Mb_substr () and mb_strcut three functions are character interception function, but substr intercept Chinese will be garbled, and then both support Chinese interception, let me introduce.

SUBSTR () function

SUBSTR (String,start,length)

String represents the object to intercept, start indicates where to start the interception, 0 means start from the beginning, positive numbers are truncated from the position of the number, negative numbers indicate the position from the end of the intercept, but still from left to right, Length indicates how long to intercept. A negative number indicates how many characters are excluded or ignored. For example:

The code is as follows Copy Code
$siteurl = ' www.bKjia.c0m ';
Print_r (substr ($siteurl, 4)); exit;

Returns: Bkjia.c0m represents the start of the 4th character from the beginning and returns all subsequent characters.

The code is as follows Copy Code

$siteurl = ' www.bKjia.c0m ';
Print_r (substr ($siteurl, -6,2)); exit;

If you want to intercept double-byte characters. Use PHP mb_substr function or Mb_strcut function, but these two functions depend on PHP extension Php_mbstring.dll component, so configure your server. That is, the PHP installation directory of the PHP_ The Mbstring.dll file is copied to the Windows/system32 directory of your Windows 2003 C drive.


As an example:

The code is as follows Copy Code

Echo mb_substr (' This way my string will not have garbled ^_^ ', 0, 7, ' utf-8 ');
?>

Output: so that my word

The code is as follows Copy Code

Echo mb_strcut (' This way my string will not have garbled ^_^ ', 0, 7, ' utf-8 ');
?>

Output: Such a

As can be seen from the above example, MB_SUBSTR is the word to divide the characters, and mb_strcut is to divide the characters by Byte, but will not produce a half-character phenomenon ...

The code is as follows Copy Code

echo Mb_substr (' Flying House blog Feihuayuan ', 0,9);
Back to: Flying court

echo Mb_substr (' Flying House blog Feihuayuan ', 0,9, ' utf-8 ');
Then return: Flying Academy Blog Feih

Mb_strcut (' Flying House blog Feihuayuan ', 0,9, ' utf-8 ');
Then return to: Flying yard
?>

For example, there is a piece of text that uses MB_SUBSTR and mb_strcut to do the slicing:

PLAIN TEXT
CODE:

The code is as follows Copy Code

$str = ' I am a long string of Chinese-';

echo "MB_SUBSTR:". Mb_substr ($str, 0, 6, ' utf-8 ');

echo "
";

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

The output results are as follows:

MB_SUBSTR: I'm a bunch of comparisons
Mb_strcut: I am

This article summarizes

From the above example can be seen, substr only support single-byte, so only for the English interception, and the MB_SUBSTR function is a double-font interception, just can be used in Chinese, and Mb_strcut is three bytes.

http://www.bkjia.com/PHPjc/631262.html www.bkjia.com true http://www.bkjia.com/PHPjc/631262.html techarticle in PHP substr (), Mb_substr () and mb_strcut three functions are character interception function, but substr intercept Chinese will be garbled, and then both support Chinese interception, let me introduce. ...

  • Related Article

    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.