php中英字串擷取

神馬和浮雲發表於2013-09-17
<?php 

@header('Content-type: text/html; charset=UTF-8');
function Ctruncate($str = '', $len = 0, $etc = '...') 
{ 
if(0 == $len) return "";

$str_len = preg_match_all('/[\x00-\x7F\xC0-\xFD]/', $str, $dummy); 
if($len >= $str_len) 
{ 
return $str; 
} 
else 
{ 
$newstr = mb_substr($str,0,$len,'utf-8'); 
return $newstr.$etc; 
} 
} 

$a = "a的flhjqowihvjnjxlkcoiqyeruqwheridsiomosdifgoiioqwerhiwerqwer";

echo Ctruncate($a, "10");

?>

 

相關文章