|
 |
18년 전 |
문자를 자를떄 Byte 기준으로 잘라 한글(2Byte)과 영문(1Byte)일때 잘린 길이가 다르게 됩니다.
그래서 한글 기준으로 문자를 잘라 잘린 길이가 비슷하게 하는 함수 입니다.
한글 8글자당 영문 19글자의 비율로 자릅니다.
/*
##############################################
::: 한글기준으로 문자 자르기 :::
사용방법 : hanCut('문자열','자를길이','말줄임표시');
ex) $str = hanCut('$str','40');
##############################################
*/
function hanCut ($str, $cut, $fix='...') {
if (!$str || strlen($str)<=$cut*2) return $str;
$han = $eng=0;
for($i=0;$i<$cut*2;$i++) {
if(ord($str[$i])>127) $han++;
else $eng++;
}
$cut = $han+$eng+(int)$eng*0.23;
if (strlen($str)<=$cut) return $str;
return preg_replace("/(([\x80-\xff].)*)[\x80-\xff]?$/", "\\1", substr($str,0,$cut)).$fix;
}
|
|
추천 : 357 |
추천
목록
|
|