查看文章 |
昨天找了一上午终于搞定,UTF-8下面截取中文字符。
2007年09月20日 星期四 11:34
此代码不是原创,我在网上搜索了一大堆,然后看到这个类似的方法,自己又改写的。 <?php
//支持中文的substr(string,length[,start]) //“反斜杠”被博客系统过滤了,请在每个 x 前面添加“反斜杠” function cnsubstr($l1,$l2,$l3=0){ $I2 = "/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]/"; preg_match_all($I2,$l1,$I3); if (count($I3[0]) - $l3 > $l2) { return implode('',array_slice($I3[0],$l3,$l2))."..."; } return implode('',array_slice($I3[0],$l3,$l2)); } //计算中文字符长度 function len($l1){ $I2 = "/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]/"; preg_match_all($I2,$l1,$I3); return count($I3[0]); } ?> 下面是在喜悦国际村看到的: 对于select ... where ... 类型的语句。
|
最近读者: