php简单的中文截取类
[code]<?phpclass CutStr
{
private $n;
private $i;
private $returnstr;
//public $utf_str;
public function __construct()
{
$this->i=0;
$this->n=0;
$this->returnstr='';
//$this->utf_str=$utfstr;
}
function cut_str_utf($utf_str,$cut_len)
{
$strlen=strlen($utf_str); //字符串字节数
while (($this->i<=$strlen) and ($this->n<$cut_len))
{
$str_tmp=substr($utf_str,$this->i,1);
$asc=Ord($str_tmp);
if($asc>=224)
{
$this->returnstr=$this->returnstr.substr($utf_str,$this->i,3);
$this->i=$this->i+3;
$this->n++;
}
elseif ($asc>=192)
{
$this->returnstr.=substr($utf_str,$this->i,2);
$this->i=$this->i+2;
$this->n++;
}
elseif ($asc>=65 && $asc<=90) //大写字母
{
$this->returnstr.=substr($utf_str,$this->i,1);
$this->i=$this->i+1;
$this->n++;
}
else
{
$this->returnstr.=substr($utf_str,$this->i,1);
$this->i=$this->i+1;
$this->n=$this->n+0.5;
}
}
if($cut_len<$strlen)
{
$this->returnstr.='............';
}
return $this->returnstr;
}
static public function getLen($utf_str='')
{
return strlen(htmlspecialchars($utf_str,ENT_QUOTES,'UTF-8'));
}
static public function convert_encode($utf_str='',$to='ISO-2022-JP')
{
if(isset($utf_str))
{
if(mb_check_encoding($utf_str,"UTF-8"))
{
mb_language("ja");
return mb_convert_encoding($utf_str,"UTF-8",$to);
//mb_encode_mimeheader($subject);
//return iconv("UTF-8",$to,$utf_str);
}
}
}
}
$str="中国是个古老的国家";
$test=new CutStr();
echo $test->cut_str_utf($str,5);
echo CutStr::getLen($str);
echo CutStr::convert_encode($str);[/code]
页:
[1]