Home > Article > Backend Development > Does php not have mb_substr()?
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer
php has the mb_substr() function. In php, mb_substr() is a function used to intercept Chinese and English strings.
mb_substr() function is not supported by default in php. We need to find and open the php.ini configuration file in the windows directory, search for "mbstring.dll
" and find " ;extension=php_mbstring.dll
" item, remove the preceding ";
" before you can use the mb_substr() function.
mb_substr() function introduction
mb_substr() function returns a part of the string, substr () function only targets English characters. If you want to split Chinese characters, you need to use mb_substr().
mb_substr ( string $str , int $start [, int $length = NULL [, string $encoding = mb_internal_encoding() ]] ) : string
Parameters | Description |
---|---|
str | Required. Extracts a substring from this string. |
start | Required. 1. Specify where to start in the string. 2. Positive number - starts at the specified position in the string Negative number - starts at the specified position from the end of the string 3. 0 - starts at the first character in the string |
length | optional. Specifies the length of the string to be returned. 1. The default is until the end of the string. 2. Positive number - returns from the position of the start parameter 3. Negative number - returns from the end of the string |
encoding | Optional. Character Encoding. If omitted, the internal character encoding is used. |
Note: If the start parameter is negative and length is less than or equal to start, length is 0.
Return value: | Returns the extracted part of the string , returns FALSE on failure, or returns an empty string. |
---|---|
PHP version: | 4 |
Usage example: Interception The first N digits of the string
<?php header('content-type:text/html;charset=utf-8'); $str="欢迎来到PHP中文网"; echo "原字符串:".$str."<br>"; echo "截取前1位:".mb_substr($str,0,1,"utf-8")."<br>"; echo "截取前2位:".mb_substr($str,0,2,"utf-8")."<br>"; ?>
<?php header('content-type:text/html;charset=utf-8'); $str="Hello world"; echo "原字符串:".$str."<br>"; echo "截取前1位:".mb_substr($str,0,1,"utf-8")."<br>"; echo "截取前2位:".mb_substr($str,0,2,"utf-8")."<br>"; ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of Does php not have mb_substr()?. For more information, please follow other related articles on the PHP Chinese website!