Home  >  Article  >  Backend Development  >  Does php not have mb_substr()?

Does php not have mb_substr()?

青灯夜游
青灯夜游Original
2022-12-23 18:36:104248browse

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.

Does php not have mb_substr()?

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().

Syntax

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.

Technical details

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$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>";
?>

Does php not have mb_substr()?

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$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>";
?>

Does php not have mb_substr()?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn