码迷,mamicode.com
首页 > Web开发 > 详细

PHP字符串截取

时间:2017-02-09 17:29:42      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:logs   result   strlen   开始   turn   return   utf8   res   class   

//字符串截取
function utf8_substr($str, $start, $length) {
    $i = 0;
    //完整排除之前的UTF8字符
    while($i < $start) {
        $ord = ord($str{$i});
        if($ord < 192) {
            $i++;
        } elseif($ord <224) {
            $i += 2;
        } else {
            $i += 3;
        }
    }
    //开始截取
    $result = ‘‘;
    while($i < $start + $length && $i < strlen($str)) {
        $ord = ord($str{$i});
        if($ord < 192) {
            $result .= $str{$i};
            $i++;
        } elseif($ord <224) {
            $result .= $str{$i}.$str{$i+1};
            $i += 2;
        } else {
            $result .= $str{$i}.$str{$i+1}.$str{$i+2};
            $i += 3;
        }
    }
    if($i < strlen($str)) {
        $result .= ‘...‘;
    }
    return $result;
}

 

PHP字符串截取

标签:logs   result   strlen   开始   turn   return   utf8   res   class   

原文地址:http://www.cnblogs.com/already/p/6383043.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!