Difference analysis between substr (), mb_substr () and mb_strcut functions in php

Source: Internet
Author: User
Difference analysis between substr (), mb_substr () and mb_strcut functions in php
This article introduces the usage and difference of the string truncation function, substr (), mb_substr () and mb_strcut in php. For more information, see.

First, let's look at the substr function. The PHP substr () function is used to split text. However, if the text to be split contains Chinese characters, the problem may occur. In this case, you can use the mb_substr/mb_strcut function. the usage of mb_substr ()/mb_strcut is similar to that of substr (). you only need to add one parameter at the end of mb_substr ()/mb_strcut, to set the encoding of the string, but the general server does not open php_mbstring.dll, need to be in php. ini opens php_mbstring.dll.

Example:

     

Output: in this way, my words

     

Output: as shown in this figure, mb_substr is used to split characters by words, while mb_strcut is used to split characters by bytes, but no half character is generated.

Description of the mbstring function: the php mbstring extension module provides multi-byte character processing capabilities. The most common feature is to use mbstring to Split Chinese characters in multiple bytes, this avoids the occurrence of half a character. due to the extension of php, it has better performance than some custom multibyte splitting functions. Mbstring extension provides several function-like functions, mb_substr and mb_strcut. let's take a look at their explanations in the manual.Mb_substrMb_substr () returns the portion of str specified by the start and length parameters. mb_substr () performs multi-byte safe substr () operation based on number of characters. position is counted from the beginning of str. first character's position is 0. second character position is 1, and so on.

Mb_strcutMb_strcut () returns the portion of str specified by the start and length parameters. mb_strcut () performs equivalent operation as mb_substr () with different method. if start position is multi-byte character's second byte or larger, it starts from first byte of multi-byte character. it subtracts string from str that is shorter than length AND character that is not part of multi-byte string or not being middle of shift sequence.

Let's take a look at the following example. We use mb_substr and mb_strcut to split a piece of text:

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

Output result: mb_substr: I am a comparison mb_strcut: I am

Let's take a closer look at the differences between the three, so that we can apply the three functions substr, mb_substr, and mb_strcut to the correct scenarios.

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.