百科狗-知识改变命运!
--

mb_strcut() - mb函数(多字节字符串转化库)

梵高12个月前 (11-21)阅读数 11#技术干货
文章标签字节

mb_strcut()

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

获取字符的一部分

说明

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 orNULLis passed, extract all bytes to the end of the string.

$encoding

mb_strcut() - mb函数(多字节字符串转化库)

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

返回值

mb_strcut()根据$start和$length参数返回$str的一部分。

更新日志

版本说明
5.4.8 PassingNULLas$lengthextracts all bytes to the end of the string. Prior to this versionNULLwas treated the same as0.

参见

  • mb_substr()获取部分字符串
  • mb_internal_encoding()设置/获取内部字符编码
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.
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.
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
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)

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)