Function name: mb_strcut()
Applicable version: PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8
Function description: The mb_strcut() function is used to intercept a string and can handle multibyte characters.
Syntax: mb_strcut(string $str, int $start, int $length = NULL, string $encoding = mb_internal_encoding()): string|false
parameter:
- $str: The string to be intercepted.
- $start: The start position of the intercepted, which can be a negative number, indicating that the calculation starts from the end of the string.
- $length (optional): The length to be intercepted, default to NULL, indicating that it is intercepted to the end of the string.
- $encoding (optional): character encoding, default to the value of mb_internal_encoding().
Return value:
- Returns the intercepted string, or returns false on failure.
Example:
$str = "Hello, 世界!"; $substring = mb_strcut($str, 0, 5); echo $substring; // 输出:Hello $substring = mb_strcut($str, 7); echo $substring; // 输出:世界!
Notes:
- By default, mb_strcut() uses the character encoding returned by the mb_internal_encoding() function, and other character encodings can be specified through the $encoding parameter.
- If the start position of the intercept exceeds the length of the string, false will be returned.
- If the $length parameter is negative, the part starting from the $start position to the end of the string is intercepted.
- The mb_strcut() function can correctly handle multibyte characters, such as Chinese characters.