╃巡洋艦㊣ 2013-09-06 08:31:00 10011次浏览 5条评论 19 5 0

Helper.php

<?php
class Helper
{
    public static function truncate_utf8_string($string, $length, $etc = '...')
    {
        $result = '';
        $string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'UTF-8');
        $strlen = strlen($string);
        for ($i = 0; (($i < $strlen) && ($length > 0)); $i++)
        {
            if ($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0'))
            {
                if ($length < 1.0)
                {
                    break;
                }
                $result .= substr($string, $i, $number);
                $length -= 1.0;
                $i += $number - 1;
            }
            else
            {
                $result .= substr($string, $i, 1);
                $length -= 0.5;
            }
        }
        $result = htmlspecialchars($result, ENT_QUOTES, 'UTF-8');
        if ($i < $strlen)
        {
            $result .= $etc;
        }
        return $result;
    }
}

将 Helper.php 放进 protected\components 文件夹下。

使用方法:

Helper::truncate_utf8_string($content,20,false); //不显示省略号
Helper::truncate_utf8_string($content,20); //显示省略号
觉得很赞
亿速云
  • 评论于 2013-11-01 17:39 举报

    这是我用的

    /**
     * 字符截取 支持UTF8/GBK
     * @param $string
     * @param $length
     * @param $dot
     */
    function str_cut($string, $length, $dot = '...') {
        $CHARSET = Yii::app()->charset;
        $strlen = strlen($string);
        if($strlen <= $length) return $string;
        $string = str_replace(array(' ','&nbsp;', '&', '"', ''', '', '', '', '<', '>', '·', ''), array(' ',' ', '&', '"', "'", '', '', '', '<', '>', '·', ''), $string);
        $strcut = '';
        if(strtolower($CHARSET) == 'utf-8') {
            $length = intval($length-strlen($dot)-$length/3);
            $n = $tn = $noc = 0;
            while($n < strlen($string)) {
                $t = ord($string[$n]);
                if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
                    $tn = 1; $n++; $noc++;
                } elseif(194 <= $t && $t <= 223) {
                    $tn = 2; $n += 2; $noc += 2;
                } elseif(224 <= $t && $t <= 239) {
                    $tn = 3; $n += 3; $noc += 2;
                } elseif(240 <= $t && $t <= 247) {
                    $tn = 4; $n += 4; $noc += 2;
                } elseif(248 <= $t && $t <= 251) {
                    $tn = 5; $n += 5; $noc += 2;
                } elseif($t == 252 || $t == 253) {
                    $tn = 6; $n += 6; $noc += 2;
                } else {
                $n++;
            }
            if($noc >= $length) {
            break;
            }
        }
        if($noc > $length) {
            $n -= $tn;
        }
        $strcut = substr($string, 0, $n);
        $strcut = str_replace(array('', '&', '"', "'", '', '', '', '<', '>', '·', ''), array(' ', '&', '"', ''', '“', '”', '—', '<', '>', '·', '…'), $strcut);
        }
        else {
            $dotlen = strlen($dot);
            $maxi = $length - $dotlen - 1;
            $current_str = '';
            $search_arr = array('&',' ', '"', "'", '“', '”', '—', '<', '>', '·', '…','∵');
            $replace_arr = array('&','&nbsp;', '"', ''', '“', '”', '—', '<', '>', '·', '…',' ');
            $search_flip = array_flip($search_arr);
            for ($i = 0; $i < $maxi; $i++) {
                $current_str = ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
                if (in_array($current_str, $search_arr)) {
                    $key = $search_flip[$current_str];
                    $current_str = str_replace($search_arr[$key], $replace_arr[$key], $current_str);
                }
                $strcut .= $current_str;
            }
        }
        return $strcut.$dot;
    }
    
    觉得很赞
  • 评论于 2014-08-27 15:08 举报

    我用的跟楼主的一样

  • 评论于 2015-06-04 22:51 举报

    yii 2 的话 该怎么操作呢?

  • 评论于 2015-08-03 13:38 举报

    问下 $length这个参数是不是指截取的长度?如果字符串是:php截取中英文 $length=4
    输出结果是:php截取
    所有问这里的‘php’长度是多少 2吗?

  • 评论于 2015-11-06 17:35 举报

    将 Helper.php 放进 protected\component s文件夹下。这个文件夹在哪里?

您需要登录后才可以评论。登录 | 立即注册