文字

mb_strcut

(PHP 4 >= 4.0.6, PHP 5)

mb_strcut获取字符的一部分

说明

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

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

参数

str

要截断的 string

start

起始位置,以字节为单位。

length

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

encoding

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

返回值

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

参见

  • mb_substr() - 获取字符串的部分
  • mb_internal_encoding() - 设置/获取内部字符编码

用户评论:

[#1] php_engineer_bk at yahoo dot com [2010-10-08 03:52:18]

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)

[#2] t dot starling at physics dot unimelb dot edu dot au [2004-08-27 04:01:54]

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.

[#3] oyag02 at yahoo dot co dot jp [2003-09-26 03:53:59]

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

上一篇: 下一篇: