PHP语言: 根据中文裁减字符串
function cutstr($string,$length, $dot = ' ...') {<BR> if (strlen($string) <= $length) {<BR> return$string;<BR> }<BR> $pre = chr(1);<BR> $end = chr(1);<BR> $string = str_replace(array('&','"', ''),array($pre . '&'. $end, $pre . '"' . $end, $pre . '' . $end), $string);<br><br> $strcut = '';<BR> if (strtolower(CHARSET) == 'utf-8'){<BR> $n = $tn =$noc = 0;<BR> while ($n< strlen($string)) {<br><br> $t =ord($string[$n]);<BR> if ($t == 9 || $t == 10 || (32<= $t && $t <= 126)) {<BR> $tn = 1;<BR> <a style="color:transparent">本@文来源gao($daima.com搞@代@#码(网5</a><strong>搞gaodaima代码</strong> $n++;<BR> $noc++;<BR> } elseif (194 <= $t&& $t <= 223) {<BR> $tn = 2;<BR> $n += 2;<BR> $noc += 2;<BR> } elseif (224 <= $t&& $t <= 239) {<BR> $tn = 3;<BR> $n += 3;<BR> $noc += 2;<BR> } elseif (240 <= $t&& $t <= 247) {<BR> $tn = 4;<BR> $n += 4;<BR> $noc += 2;<BR> } elseif (248 <= $t&& $t <= 251) {<BR> $tn = 5;<BR> $n += 5;<BR> $noc += 2;<BR> } elseif ($t == 252 || $t ==253) {<BR> $tn = 6;<BR> $n += 6;<BR> $noc += 2;<BR> } else {<BR> $n++;<BR> }<br><br> if ($noc >= $length){<BR> break;<BR> }<BR> }<BR> if ($noc> $length) {<BR> $n -= $tn;<BR> }<BR> $strcut =substr($string, 0, $n);<BR> } else {<BR> for ($i =0; $i < $length; $i++) {<BR> $strcut .= ord($string[$i])> 127 ? $string[$i] . $string[++$i] : $string[$i];<BR> }<BR> }<BR> $strcut = str_replace(array($pre . '&' .$end, $pre . '"' . $end, $pre . '' .$end), array('&', '"',''), $strcut);<br><br> $pos = strrpos($strcut, chr(1));<BR> if ($pos !== false) {<BR> $strcut =substr($strcut, 0, $pos);<BR> }<BR> return $strcut . $dot;<BR>}<BR>