php自动截取中文字符串 和 取出html、css、js格式函数

时间:2021-06-03 07:47:52

//中文字符串截取方法 
function ezk_substr($string, $start = 0, $sublen, $code = 'utf-8'){
 //$string 要截取的字符串   $start  截取时的开始位置   $sublen 截取的长度   $code 截取的字符格式
    if($code == 'utf-8') {
        $pa = "/[\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($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< $strlen; $i++) {
            if($i>=$start && $i< ($start+$sublen)) {
                if(ord(substr($string, $i, 1))>129) {
                    $tmpstr.= substr($string, $i, 2);
                }else {
                    $tmpstr.= substr($string, $i, 1);
                }
            }
            if(ord(substr($string, $i, 1))>129) $i++;
        }
        if(strlen($tmpstr)< $strlen ) $tmpstr.= "...";
        return $tmpstr;
    }
}

//去除html标签、css样式、js、空格等格式的功能(格式化html文本)
function ezk_cutstr_html($string){
 $string = strip_tags($string);
 $string = preg_replace ('/\n/is', '', $string);
 $string = preg_replace ('/ | /is', '', $string);
 $string = preg_replace ('/&nbsp;/is', '', $string);
 
 preg_match_all("/[\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]/", $string, $t_string);  
 
   return $string;
}


扎客小站:www.ezhake.com