EUC-KR
[code type=php]
function cutstr ($strTmp=0, $max=0,$flag=0)
{
$len = strlen($strTmp);
if ($len <= $max || $max == 0 ) return $strTmp;
for ($i=0; $i<$len;) {
if (ord(substr($strTmp,$i,1)) <= 127)
{
$check=0; $i++;
}else{
$check=1; $i+=2;
}
if ( $i >= $max) break;
}
if ($check) $i-=2;
if( $flag ){
return substr($strTmp,0,$i);
}else{
$strTmp = substr($strTmp,0,$i)."...";
return $strTmp;
}
}
?>
UTF-8
[code type=php]
function cutstr ($str=0, $max=0,$flag=0)
{
$len = strlen($str);
if ($len <= $max || $max == 0 ) return $str;
for ($i = 0; $i < $len; ) {
$high = ord($str{$i});
if( $i >= $max ) break;
if ($high < 0x80)
$i += 1;
else if ($high < 0xE0)
$i += 2;
else if ($high < 0xF0)
$i += 3;
else
$i += 4;
echo $i."
";
}
if( $flag ){
return substr($str,0,$i);
}else{
$str = substr($str,0,$i)."...";
return $str;
}
}
?>
Home // Blog
Home // Notice
Home // Tag Log
Home // Location Log
Home // Media Log
Home // GuestBook
문자열 자르기 함수 euc-kr, utf-8 버전
Posted at 2007/01/25 00:12 //
in 웹프로그래밍™/PHP, ASP //
by