╃巡洋艦㊣ 2011-06-29 14:17:40 11676次浏览 38条回复 3 0 0

Helper.php

<?php
class Helper extends CController
{
  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);  //显示省略号

[attach]221[/attach]

上面是Helper.php的源码,欢迎下载!

您需要登录后才可以回复。登录 | 立即注册