PHP: function msubstr($str,$start,$len) { $strlen=$start+$len; for($i=0;$i<$strlen;$i++) { if(ord(substr($str,$i,1))>0xa0) { $tmpstr.=substr($str,$i,2); $i++; } else $tmpstr.=substr($str,$i,1); } return $tmpstr; } ------------------------------------- PHP: <?PHP $str="这个字符好长呀,^_^"; $Short_Str=showShort($str,4);//截取前面4个汉字,结果为:这个字符... Echo "$Short_Str"; Function csubstr($str,$start,$len) { $strlen=strlen($str); $clen=0; for($i=0;$i<$strlen;$i++,$clen++) { if ($clen>=$start+$len) break; if(ord(substr($str,$i,1))>0xa0) { if ($clen>=$start) $tmpstr.=substr($str,$i,2); $i++; } else { if ($clen>=$start) $tmpstr.=substr($str,$i,1); } } return $tmpstr; } Function showShort($str,$len) { $tempstr = csubstr($str,0,$len); if ($str<>$tempstr) $tempstr .= "..."; //要以什么结尾,修改这里就可以. return $tempstr; }?>
PHP: <?php /***********************************************************************/ /* */ /* Author:CnPhper */ /* HomePage: <a href="http://www.cnphper.com" target="_blank">www.cnphper.com</a> */ /* Email: [email protected] */ /* QQ:1984412 */ /* */ /***********************************************************************/ function get_substr($string,$start='0',$length='') { $start = (int)$start; $length = (int)$length; $i = 0; if(!$string) { return; } if($start>=0) { while($i<$start) { if(ord($string[$i])>127) { $i = $i+2; } else { $i++; } } $start = $i; if($length=='') { return substr($string,$start); } elseif($length>0) { $end = $start+$length; while($i<$end) { if(ord($string[$i])>127) { $i = $i+2; } else { $i++; } } if($end != $i-1) { $end = $i; } else { $end--; } $length = $end-$start; return substr($string,$start,$length); } elseif($length==0) { return; } else { $length = strlen($string)-abs($length)-$start; return get_substr($string,$start,$length); } } else { $start = strlen($string)-abs($start); return get_substr($string,$start,$length); } } ?>
PHP: function cnSubstr($string,$strlen,$other=true) { if ( function_exists('mb_substr') ) { $rstr = mb_substr($string,0,$strlen); if (mb_strlen($string)>$strlen && $other) { $rstr .='...'; } } else { for($i=0;$i<$strlen;$i++) { if(ord(substr($string,$i,1))>0xa0) { $j++; } if($j%2!=0) { $strlen++; } $rstr = substr($string,0,$strlen); if (strlen($string)>$strlen && $other) { $rstr .='...'; } } } return $rstr;}