方法一
function dsubstr($string, $length, $suffix = '', $start = 0) {
if($start) {
$tmp = dsubstr($string, $start);
$string = substr($string, strlen($tmp));
}
$strlen = strlen($string);
if($strlen <= $length) return $string;
$string = str_replace(array('"', '<', '>'), array('"', '<', '>'), $string);
$length = $length - strlen($suffix);
$str = '';
if('UTF-8' == 'UTF-8') {
$n = $tn = $noc = 0;
while($n < $strlen) {
$t = ord($string{$n});
if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
$tn = 1; $n++; $noc++;
} elseif(194 <= $t && $t <= 223) {
$tn = 2; $n += 2; $noc += 2;
} elseif(224 <= $t && $t <= 239) {
$tn = 3; $n += 3; $noc += 2;
} elseif(240 <= $t && $t <= 247) {
$tn = 4; $n += 4; $noc += 2;
} elseif(248 <= $t && $t <= 251) {
$tn = 5; $n += 5; $noc += 2;
} elseif($t == 252 || $t == 253) {
$tn = 6; $n += 6; $noc += 2;
} else {
$n++;
}
if($noc >= $length) break;
}
if($noc > $length) $n -= $tn;
$str = substr($string, 0, $n);
} else {
for($i = 0; $i < $length; $i++) {
$str .= ord($string{$i}) > 127 ? $string{$i}.$string{++$i} : $string{$i};
}
}
$str = str_replace(array('"', '<', '>'), array('"', '<', '>'), $str);
return $str == $string ? $str : $str.$suffix;
}
$content="该公司声明 还指e出,这一???航线并非永久性取消。与停止直飞北京的服务一样,公司将寻求美国运输部门的批准,暂停芝加哥与上海之间的航线,以便一旦条件改善,该直飞服务能重返市场。该公司表示,期待在2019年北京大兴机场投入使用时,能找到新的合作方式。";
echo dsubstr($content,27);
方法二
$text = "欢迎访问sina新浪播客";
$value = substr($text, 0, 18);
$value_length = strlen($value);
$value_count = 0;
for ($i = 0; $i < $value_length; $i++)
{
if (ord($value{$i}) > 127)
{
$value_count++;
}
}
if ($value_count % 2 != 0)
{
$value = substr($text, 0, $value_length - 1);
}
echo $value."
";
?>