截斷中文字避免亂碼 Cut Chinese Characters

//截斷中文字並且避免結尾亂碼,但是就有可能會少一個字, 無解
function utf8_substr($StrInput,$strStart,$strLen){
    //對字串做URL Eecode
    $StrInput = mb_substr($StrInput,$strStart,mb_strlen($StrInput));
    $iString = urlencode($StrInput);
    $lstrResult="";
    $istrLen = 0;
    $k = 0;
    do{
        $lstrChar = substr($iString, $k, 1);
        if($lstrChar == "%"){
            $ThisChr = hexdec(substr($iString, $k+1, 2));
            if($ThisChr >= 128){
                if($istrLen+3 < $strLen){
                    $lstrResult .= urldecode(substr($iString, $k, 9));
                    $k = $k + 9;
                    $istrLen+=3;
                }else{
                    $k = $k + 9;
                    $istrLen+=3;
                }
            }else{
                $lstrResult .= urldecode(substr($iString, $k, 3));
                $k = $k + 3;
                $istrLen+=2;
            }
        }else{
            $lstrResult .= urldecode(substr($iString, $k, 1));
            $k = $k + 1;
            $istrLen++;
        }
    }while ($k < strlen($iString) && $istrLen < $strLen);

    return $lstrResult;
}

https://blog.xuite.net/chenbruse/bruse/13351768-%E8%A7%A3%E6%B1%BAPHP+substr%E5%88%87%E5%89%B2%E4%B8%AD%E6%96%87%E5%AD%97%E5%95%8F%E9%A1%8C

自已寫的

function cut_string($str, $_length){
	$count = 0;
	$length = mb_strlen($str, 'utf-8');
	$new_str = '';

	for($i = 0; $i < $length; $i++){
		$char = mb_substr($str, $i, 1, 'utf-8');
		$b_length = strlen($char);
		if($b_length > 1){
			$count += 2;
		}else{
			$count ++;
		}

		if($count <= $_length){
			$new_str .= $char;
		}
	}

	return $new_str;
}