PHP截取编码为utf8的字符串

180it 2020-02-18 PM 1062℃ 0条

/**

  • 截取编码为utf8的字符串
    *
  • @param string $strings 预处理字符串
  • @param int $start 开始处 eg:0
  • @param int $length 截取长度
    */

function subString($strings, $start, $length) {

if (function_exists('mb_substr') && function_exists('mb_strlen')) {
    $sub_str = mb_substr($strings, $start, $length, 'utf8');
    return mb_strlen($sub_str, 'utf8') < mb_strlen($strings, 'utf8') ? $sub_str . '...' : $sub_str;
}
$str = substr($strings, $start, $length);
$char = 0;
for ($i = 0; $i < strlen($str); $i++) {
    if (ord($str[$i]) >= 128)
        $char++;
}
$str2 = substr($strings, $start, $length + 1);
$str3 = substr($strings, $start, $length + 2);
if ($char % 3 == 1) {
    if ($length <= strlen($strings)) {
        $str3 = $str3 .= '...';
    }
    return $str3;
}
if ($char % 3 == 2) {
    if ($length <= strlen($strings)) {
        $str2 = $str2 .= '...';
    }
    return $str2;
}
if ($char % 3 == 0) {
    if ($length <= strlen($strings)) {
        $str = $str .= '...';
    }
    return $str;
}

}

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

PHP截取编码为utf8的字符串