- It must be possible for Chinese, English and special characters.
- //utf-8 Chinese interception, single-byte interception mode
- function cn_substr_utf8($str,$length,$append='...',$start=0){
- if(strlen($str)<$ start+1){
- return '';
- }
- preg_match_all("/./su",$str,$ar);
- $str2='';
- $tstr='';
- for($i=0 ;isset($ar[0][$i]);$i++){
- if(strlen($tstr)<$start){
- $tstr.=$ar[0][$i];
- }else {
- if(strlen($str2)<$length + strlen($ar[0][$i])){
- $str2.=$ar[0][$i];
- }else{
- break;
- }
- }
- }
- return $str==$str2?$str2:$str2.$append;
- }
- //----Usage examples are as follows:
- echo cn_substr_utf8('I am a, and ha, oh Also,,country!',12);
Copy code
|