Home > Backend Development > PHP Tutorial > PHP distinguishes string lengths under different encodings_PHP tutorial

PHP distinguishes string lengths under different encodings_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-21 15:43:56
Original
955 people have browsed it

The Chinese string of UTF-8 is three bytes

Copy code The code is as follows:

//Encoding UTF-8
echo strlen('test text a test text');
echo '-';
echo mb_strlen('test text a test text', 'utf-8');
?>
Output: 25-9

The Chinese string of GB2312 is two bytes
Copy code The code is as follows:

//Encoding GB2312
echo strlen('Test text a test text');
echo '-';
echo mb_strlen('test text a test text','Gb2312');
?>

Output: 17- 9
In Mysql database (versions after 5.1), if the field type is varchar(10), 10 characters (not bytes) can be inserted;
So when judging the length of the string, you need to encode it according to the document to distinguish.
symbol is a simple string interception under UTF-8 (interception based on the number of characters)
Copy code The code is as follows:

/*
* UTF-8 string interception
* $str String to intercept
* $start interception starting position
* $length interception length
*/
function cutStr($str,$start,$length) {
$restr = '';
$j = 0;
$end = $length + $start - 1;
$plen = strlen($str);
for($i=0;$i<$plen;$i++) {
$restr .= ord($str[$i]) >127 ? $str[$i].$str[++$i].$str[++$i] : $str[$i];
$j++;
if ($j < ; $start){$restr = '';}
if ($j >= $end){break;}
}
$restr .='';
return $restr;
}
$str = 'China News Service, September 24th. The third financial summit of leaders of the Group of Twenty (G20) will be held in Pittsburgh, USA today. ';
echo $str;
echo '
';
echo utf8_substr($str,0,25);
echo '
';
?> ;

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320670.htmlTechArticleThe Chinese string of UTF-8 is three bytes. Copy the code as follows: ?php //Encoding UTF- 8 echo strlen('test text a test text'); echo '-'; echo mb_strlen('test text a test text',...
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template