Home > Backend Development > PHP Tutorial > PHP interception Chinese string function ~ including GB2312 and UT-8

PHP interception Chinese string function ~ including GB2312 and UT-8

巴扎黑
Release: 2016-11-24 10:38:35
Original
890 people have browsed it

/*
Chinese character interception function supported by Utf-8 and gb2312
cut_str(string, interception length, starting length, encoding);
The default encoding is utf-8
The default starting length is 0
* /

function cut_str($string, $sublen, $start = 0, $code = 'UTF-8')
{
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]/”; (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 ;
{
                                                                                                                                                                                                                                                                                                              substr($string, $i, 1);                                        If(strlen($tmpstr)< $strlen ) $tmpstr.= "...";
                                                                                         return $tmpstr; ; $start + $len;
for($i = 0; $i < $strlen; $i++) {
                                                                                     = substr($str, $i, 2);                                    Return $tmpstr;
}
?> xC0-xFF][x80-xBF]+){0,'.$from.'}'.
                                                      '.$len.'}).*#s',
                                                                                                                         

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template