跳至
function cutstr($string, $length, $dot = ' ...')
{
global $charset;
if(strlen($string) '), array('&', '"', ''), $string);
$strcut = '';
if(strtolower($charset) == 'utf-8')
{
$n = $tn = $noc = 0;
while($n $length) $n -= $tn;
$strcut = substr($string, 0, $n);
}
else
{
for($i = 0; $i 127 ? $string[$i] . $string[++$i] : $string[$i];
}
}
$strcut = str_replace(array('&', '"', ''), array('&', '"', ''), $strcut);
return $strcut . $dot;
}
2. [代码]使用方法
$testStr = "好好学习!(good good study)天天向上!(day day up)"; // ^_^! V
// 使用utf文档
echo cutstr($testStr, 10);
// 使用dos文档使用以下代码测试utf-8的效果
//$testStr = iconv('GBK', 'UTF-8', $testStr);
//echo iconv('UTF-8', 'GBK', cutstr($testStr, 10));