The PHP Online Conference 2021

mb_substr

(PHP 4 >= 4.0.6, PHP 5, PHP 7)

mb_substr获取部分字符串

说明

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

根据字符数执行一个多字节安全的 substr() 操作。 位置是从 str 的开始位置进行计数。 第一个字符的位置是 0。第二个字符的位置是 1,以此类推。

参数

str

从该 string 中提取子字符串。

start

如果 start 不是负数,返回的字符串会从 strstart 的位置开始,从 0 开始计数。举个例子,字符串 'abcdef',位置 0 的字符是 'a',位置 2 的字符是 'c',以此类推。

如果 start 是负数,返回的字符串是从 str 末尾处第 start 个字符开始的。

length

str 中要使用的最大字符数。如果省略了此参数或者传入了 NULL,则会提取到字符串的尾部。

encoding

encoding 参数为字符编码。如果省略,则使用内部字符编码。

返回值

mb_substr() 函数根据 startlength 参数返回 str 中指定的部分。

更新日志

版本 说明
5.4.8 length 传入 NULL,则从 start 提取到字符串的结尾处。 在之前的版本里, NULL 会被当作 0 来处理。

参见

add a note add a note

User Contributed Notes 9 notes

up
37
Silvan
13 years ago
Passing null as length will not make mb_substr use it's default, instead it will interpret it as 0.
<?php
mb_substr
($str,$start,null,$encoding); //Returns '' (empty string) just like substr()
?>
Instead use:
<?php
mb_substr
($str,$start,mb_strlen($str),$encoding);
?>
up
9
xiaogil at yahoo dot fr
15 years ago
Thanks Darien from /freenode #php for the following example (a little bit changed).

It just prints the 6th character of $string.
You can replace the digits by the same in japanese, chinese or whatever language to make a test, it works perfect.

<?php
mb_internal_encoding
("UTF-8");
$string = "0123456789";
$mystring = mb_substr($string,5,1);
echo
$mystring;
?>

(I couldn't replace 0123456789 by chinese numbers for example here, because it's automatically converted into latin digits on this website, look :
&#38646;&#19968;&#20108;&#19977;&#22235;
&#20116;&#20845;&#19971;&#20843;&#20061;)

gilv
up
6
qbolec at gmail dot com
5 years ago
As you often need to iterate over UTF-8 characters inside a string, you might be tempted to use mb_substr($text,$i,1).
The problem with this is that there is no "magic" way to find $i-th character inside UTF-8 string, other than reading it byte by byte from the begining. Thus a loop which calls mb_substr($text,$i,1) N times for all possible N values of $i, will take much longer than expected. The larger the $i gets, the longer is the search for $i-th letter. As characters are between 1 to 6 bytes long, one can convince oneself, that the execution time of such loop is actually Theta(N^2), which can be really slow even for moderately long texts.
One way to work around it is to first split your text into an array of letters using some smart preprocessing, and only then iterate over the array.
Here is the idea:
<?php
class Strings
{
  public static function
len($a){
    return
mb_strlen($a,'UTF-8');
  }
  public static function
charAt($a,$i){
    return
self::substr($a,$i,1);
  }
  public static function
substr($a,$x,$y=null){
    if(
$y===NULL){
     
$y=self::len($a);
    }
    return
mb_substr($a,$x,$y,'UTF-8');
  }
  public static function
letters($a){
   
$len = self::len($a);
    if(
$len==0){
      return array();
    }else if(
$len ==