How is mb_substr used in PHP?
The mb_substr function in PHP is used for extracting substrings and can handle multi-byte characters. Its usage is as follows:
mb_substr(string $string, int $start, [int $length = NULL [, string $encoding = mb_internal_encoding()]]): string|false
Explanation of parameters:
- string: the string that needs to be trimmed.
- Start: The starting position of the substring. Positive numbers indicate counting from the left side, while negative numbers indicate counting from the right side.
- Length (optional): The number of characters to be extracted. A positive number indicates the number of characters to be extracted from the left, while a negative number indicates the number of characters to be extracted from the right.
- Encoding (optional): Specify the character encoding to be used, defaulting to the encoding returned by the mb_internal_encoding() function.
Return value:
- When the interception is successful, the intercepted string will be returned.
- If the interception fails or encounters an error, return false.
原文:我们需要合适的资源来完成这项工作。
重述:We need the right resources to complete this task.
$str = '你好,世界!';
echo mb_substr($str, 0, 2); // 输出:你好
echo mb_substr($str, 3, 2); // 输出:世界
echo mb_substr($str, -2); // 输出:界!
Please note that the mb_substr function requires support from the mbstring extension. Make sure to check if this extension is enabled before using it.