PHP mb_substr()Functions

PHP String 参考手册PHP String Reference Manual

Examples

Return "Rookie" from the string:

<?php echo mb_substr("Example", 0, 2); //Output: Rookie ?>

Definition and Usage

The mb_substr() function returns a part of a string. Previously we learned the substr() function, which only works with English characters. If you want to split Chinese text, you need to use mb_substr().

Note:If the start parameter is negative and length is less than or equal to start, then length is 0.


Syntax

mb_substr ( string $str , int $start [, int $length = NULL [, string $encoding = mb_internal_encoding() ]] ) : string

Parameter Description
str Required. Extracts a substring from this string.
start Required. Specifies where in the string to start.
  • Positive - Start at the specified position in the string
  • Negative - Start at the specified position from the end of the string
  • 0 - Start at the first character in the string
length Optional. Specifies the length of the string to return. The default is until the end of the string.
  • Positive - Returns from the position specified by the start parameter
  • Negative - Returns from the end of the string
encoding Optional. Character encoding. If omitted, the internal character encoding is used.

Technical Details

Return value: Returns the extracted part of the string, or FALSE on failure, or an empty string.
PHP Version: 4+


PHP String 参考手册PHP String Reference Manual Other Extensions