The difference between strlen,mb_strlen,count in PHP

Source: Internet
Author: User

Transfer from http://www.cnblogs.com/mo-beifeng/archive/2011/08/09/2133039.html

This is not a fixed number. This article briefly describes the restriction rules.

strlen calculate string length, one Chinese when 2 characters

Mb_strlen according to its character encoding pattern, the statistic character quot

Count counts the number of elements in an array or the number of attributes in an object

<?php
Header (' Content-type:text/html;charset=utf-8 ');
$string 1 = "Xiechun industry";//define Chinese character variable
$string 2= "Xcy";//define English character variables
Direct output look at their lengths
Echo strlen ($string 1);
echo "</br>";
Echo strlen ($string 2);
echo "</br>";

Try Mb_strlen with PHP multi-byte extension function
Echo Mb_strlen ($string 1, ' UTF8 ');
echo "</br>";
Echo Mb_strlen ($string 2, ' UTF8 ');
echo "</br>";

?>

The output is:

9
3
3
3

1. Restriction rules

The restriction of a field has the following rules when the field is defined:

A) storage Limits

The varchar field stores the actual content separately from the clustered index, and the content begins with 1 to 2 bytes representing the actual length (2 bytes in length over 255), so the maximum length cannot exceed 65535.  

b) encoding length limit

If the character type is GBK, each character can be up to 2 bytes, and the maximum length cannot exceed 32766.

If the character type is UTF8, each character can be up to 3 bytes and the maximum length cannot exceed 21845.

For a more English-speaking forum , use GBK to occupy 2 bytes per character, while using UTF-8 in English only takes one byte.

If the limit above is defined, the varchar field is forcibly converted to the text type and generates warning.  

c) line length limit

The length of a row definition is the limit of the varchar length in the actual application. MySQL requires that a row's definition length cannot exceed 65535. If the defined table length exceeds this value, the prompt

ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have the to change some columns to TEXT or BLOBs.  

2. Calculation example

Give two examples to illustrate the actual length of the calculation.  

A) if a table has only one varchar type, as defined as

CREATE table t4 (c varchar (N)) CHARSET=GBK;

The maximum value for n here is (65535-1-2)/2= 32766.

The reason for minus 1 is that the actual row storage starts with the second byte ';

The reason for minus 2 is that the 2 bytes of the varchar header represent the length;

The reason for the addition of 2 is that the character encoding is GBK.

b) If a table is defined as

CREATE table t4 (c int, C2 char (+), C3 varchar (N)) Charset=utf8;

The maximum value of n here is (65535-1-2-4-30*3)/3=21812

Minus 1 and minus 2 are the same as in the previous example;

The reason for minus 4 is that the int type C accounts for 4 bytes;

The reason for reducing 30*3 is that char (30) occupies 90 bytes and the encoding is UTF8.  

If the varchar exceeds the B rule above, and is strongly turned into the text type, then each field occupies a defined length of 11 bytes, which is not "varchar", of course.

The difference between strlen,mb_strlen,count in PHP

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.