UseBB 中文字符截断函数(UTF-8)修改

来自站长百科
跳转至: 导航、​ 搜索

导航: 上一页

UseBB中在首页最后发表要用到一个函数entities_rtrim,由于UseBB是采用的ISO-8859-1编码,所以当使用中文的时候,最后一个字符会被截断,进而照成乱码。所以,将该函数修改了一下,以适应中文版式。

找到UseBB/sources/functions.php,查找函数:*function entities_rtrim*

function entities_rtrim($string, $length) {

 $new_string = '';
 $new_length = $pos = 0;
 $entity_open = false;

 if ( function_exists('mb_language') && mb_language() != 'neutral') {

 $strlen = 'mb_strlen';
 $substr = 'mb_substr';

 } else {

 $strlen = 'strlen';
 $substr = 'substr';

 }

 while ( $pos < $strlen($string) && ( $new_length < $length ||
$entity_open ) ) {

 $char = $substr($string, $pos, 1);

 if ( $char == '&' ) {

 $entity_open = true;

 } elseif ( $char == ';' && $entity_open ) {

 $entity_open = false;
 $new_length++;

 } elseif ( !$entity_open ) {

 $new_length++;

 }

 $new_string .= $char;
 $pos++;

 }

 return $new_string;

}

替换成:

function entities_rtrim($str, $lenth)
{
 $start=0;
 $len = strlen($str);
 $r = array();
 $n = 0;
 $m = 0;
 for($i = 0; $i < $len; $i++)
 {
 $x = substr($str, $i, 1);
 $a = base_convert(ord($x), 10, 2);
 $a = substr('00000000'.$a, -8);
 if ($n < $start)
 {
 if (substr($a, 0, 1) == 0)
 {
 }
 elseif (substr($a, 0, 3) == 110)
 {
 $i += 1;
 }
 elseif (substr($a, 0, 4) == 1110)
 {
 $i += 2;
 }
 $n++;
 }
 else
 {
 if (substr($a, 0, 1) == 0)
 {
 $r[] = substr($str, $i, 1);
 }
 elseif (substr($a, 0, 3) == 110) {
 $r[] = substr($str, $i, 2);
 $i += 1;
 }elseif (substr($a, 0, 4) == 1110) {
 $r[] = substr($str, $i, 3);
 $i += 2;
 }else{
 $r[] = '';
 }
 if (++$m >= $lenth){
 break;
 }
 }
 }
 $trunstr=join('',$r);

 return $trunstr;

}

相关条目[ ]

参考来源[ ]