_php techniques for string length discrimination under different coding codes in PHP

Source: Internet
Author: User
UTF-8 's Chinese string is three bytes
Copy Code code as follows:

<?php
Coding UTF-8
echo strlen (' Test text a Test text ');
Echo '-';
echo mb_strlen (' Test text a Test text ', ' utf-8 ');
?>
Output: 25-9

GB2312 's Chinese string is two bytes
Copy Code code as follows:

<?php
Coding GB2312
echo strlen (' Test text a Test text ');
Echo '-';
echo mb_strlen (' Test text a Test text ', ' Gb2312 ');
?>

Output: 17-9
In the MySQL database (later version 5.1), you can insert 10 characters (not bytes) If the field type is varchar (10);
Therefore, it is necessary to distinguish the length of the string according to the document encoding.
Character a simple UTF-8 string interception (by the number of characters to intercept)
Copy Code code as follows:

?
/*
* UTF-8 string Intercept
* $str the string to intercept
* $start intercept start position
* $length Intercept 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 = ' Beijing, September 24 electric Power Group of 20 (G20) leaders of the third financial summit will be held in the United States Pittsburg today. ';
Echo $str;
Echo ' <br> ';
Echo utf8_substr ($str, 0,25);
Echo ' <br> ';
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.