mb_strcut

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

mb_strcut获取字符的一部分

说明

mb_strcut ( string $str , int $start , int $length = NULL , string $encoding = mb_internal_encoding() ) : string

mb_strcut()mb_substr() 类似,都是从字符串中提取子字符串,但是按字节数来执行,而不是字符个数。 如果截断位置位于多字节字符两个字节的中间,将于该字符的第一个字节开始执行。 这也是和 substr() 函数的不同之处,后者简单地将字符串在字节之间截断,这将导致一个畸形的字节序列。

参数

str

要截断的 string

start

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

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

length

字节长度。If omitted or NULL is passed, extract all bytes to the end of the string.

encoding

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

返回值

mb_strcut() 根据 startlength 参数返回 str 的一部分。

更新日志

版本 说明
5.4.8 Passing NULL as length extracts all bytes to the end of the string. Prior to this version NULL was treated the same as 0.

参见

User Contributed Notes

olivthill at gmail dot com 16-Nov-2017 02:30
Here is an example with UTF8 characters, to see how the start and length arguments are working:

  $str_utf8 = utf8_encode("Déjà_vu");
  $str_utf8_0 = mb_strcut($str_utf8, 0, 4, "UTF-8"); // Déj
  $str_utf8_1 = mb_strcut($str_utf8, 1, 4, "UTF-8"); // éj
  $str_utf8_2 = mb_strcut($str_utf8, 2, 4, "UTF-8"); // éj
  $str_utf8_3 = mb_strcut($str_utf8, 3, 4, "UTF-8"); // jà_
  $str_utf8_4 = mb_strcut($str_utf8, 4, 4, "UTF-8"); // à_v

The string includes two special charaters, "é" and "à" internally coded with two bytes.
Note that a multibyte character is removed rather than kept in half at the end of the output.
Note also that the result is the same for a cut 1,4 and a cut 2,4 with this string.
php_engineer_bk at yahoo dot com 08-Oct-2010 03:52
function cut_sense($matne_harf, $l_harf ,$return=1 ) {
if ( strlen($matne_harf) > $l_harf){
$end='...';
}else{
$end='';
}
    if ( function_exists('mb_strcut') ){
        $matne_harf = mb_strcut ( $matne_harf, 0 , $l_harf , "UTF-8" );
    }else{
        $matne_harf =substr($matne_harf, 0, $l_harf);
    }
$text=''.$matne_harf.''.$end.'';
  if ( $return == 1){
  return $text;
  }else{
  print $text;
  }
}

Iranian php programmer (farhad zand +989383015266)
t dot starling at physics dot unimelb dot edu dot au 27-Aug-2004 04:01
What the manual and the first commenter are trying to say is that mb_strcut uses byte offsets, as opposed to mb_substr which uses character offsets.

Both mb_strcut and mb_substr appear to treat negative and out-of-range offsets and lengths in the basically the same way as substr. An exception is that if start is too large, an empty string will be returned rather than FALSE. Testing indicates that mb_strcut first works out start and end byte offsets, then moves each offset left to the nearest character boundary.
oyag02 at yahoo dot co dot jp 26-Sep-2003 03:53
diffrence between mb_substr and mb_substr

example:
mb_strcut('I_ROHA', 1, 2) returns 'I_'. Treated as byte stream.
mb_substr('I_ROHA', 1, 2) returns 'ROHA' Treated as character stream.

# 'I_' 'RO' 'HA' means multi-byte character
PHP8中文手册 站长在线 整理 版权归PHP文档组所有