How to use PHP to determine the length range of a varchar field that does not exceed MySQL input

Source: Internet
Author: User
This article mainly describes how to use PHP to determine the input does not exceed the length of the MySQL field of the varchar range, has a certain reference value, now share to everyone, the need for friends can refer to

varchar Type field, if you set the length to 10, then both Chinese characters and English can save 10.

However, if the Utf-8 encoding, a Chinese character is 3 characters in length, such as the string $str= "Hello Ah!!";
If you use the Strlen function to judge that the length is 11, just over the length of the varchar, but in fact it is not so, if directly into the phpMyAdmin inside the INSERT statement, this string can be inserted!
For a database, its length is 5, then how do we use PHP to get this length? Use the Iconv_strlen () function!

Echo Iconv_strlen ($str, ' utf-8′);

Note that the second argument is the current character set, so that depending on the character set, the result you get is calculated from one character per length!
The above statement, will output 5, how, you will judge it?

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.