본문 바로가기
[Program]/[PHP]

php 문자열 자르기 소스

by 봉필v 2014. 8. 25.

// 글자 자르기
function strCut($str, $len, $suffix="…")
{
    $s = substr($str, 0, $len);
    $cnt = 0;
    for ($i=0; $i<strlen($s); $i++)
        if (ord($s[$i]) > 127)
            $cnt++;

    $s = substr($s, 0, $len - ($cnt % 3));

    if (strlen($s) >= strlen($str))
        $suffix = "";
    return $s . $suffix;
}

 

echo strCut("문자열열열열열열열","5");

 

 

또는

 

 

function strcut($str, $ecut, $estr='...', $scut='0', $type='UTF-8')
{
 if( mb_strlen($str, $type) <= $ecut ) return $str;
    return mb_substr($str, $scut, $ecut, $type).$estr;
}

 

 

echo strcut("문자열열열열열열열","5")