欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

一个很好的中文截取字符窜函数

程序员文章站 2022-06-10 18:42:57
...

无详细内容 无 源码与演示: 源码出处演示出处 function cut_str($string,$sublen,$filter=true,$start=0,$code='UTF-8'){if($filter) $string=Html2Text($string);if($code=='UTF-8'){$pa="/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[

源码与演示:源码出处 演示出处

function cut_str($string,$sublen,$filter=true,$start=0,$code='UTF-8'){
	if($filter) $string=Html2Text($string);
	if($code=='UTF-8'){
		$pa="/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
		preg_match_all($pa,$string,$t_string);
		if(count($t_string[0])-$start>$sublen) return join('',array_slice($t_string[0],$start,$sublen))."...";
		return join('',array_slice($t_string[0],$start,$sublen));
	}else{
		$start=$start*2;
		$sublen=$sublen*2;
		$strlen=strlen($string);
		$tmpstr='';
		for($i=0;$i=$start&&$i129){
					$tmpstr.=substr($string,$i,2);
				}else{
					$tmpstr.=substr($string,$i,1);
				}
			}
			if(ord(substr($string,$i,1))>129) $i++;
		}
		if(strlen($tmpstr)||/isU","",$str);
	$alltext = "";
	$start = 1;
	for($i=0;$i"){
			$start = 1;
		}else if($start==1){
			if($str[$i]=="31){
				$alltext .= $str[$i];
			}
		}
	}
	$alltext = str_replace(" "," ",$alltext);
	$alltext = preg_replace("/&([^;&]*)(;|&)/","",$alltext);
	$alltext = preg_replace("/[ ]+/s"," ",$alltext);
	return $alltext;
}