Use mb in php

Source: Internet
Author: User
Recently, a secure insertion and Security Update method has been added to the data volume class for security purposes. Filter dangerous strings before inserting data, and check the data type and length. The problem arises. In terms of length check, the database uses varchar and the length is calculated by the number of characters. For example, abcdef is counted as 6 characters, and the fifth or sixth character is counted as six characters in 1234. While

Recently, a secure insertion and Security Update method has been added to the data volume class for security purposes. Filter dangerous strings before inserting data, and check the data type and length. The problem arises. In terms of length check, the database uses varchar and the length is calculated by the number of characters. For example, abcdef is counted as 6 characters, and the fifth or sixth character is counted as six characters in 1234. While

Recently, a secure insertion and Security Update method has been added to the data volume class for security purposes. Filter dangerous strings before inserting data, and check the data type and length.

The problem arises. In terms of length check, the database uses varchar and the length is calculated by the number of characters. For example, "abcdef" is counted as 6 characters, "1234 five or six" is also six characters. The strlen method of php is not the same, strlen ("abcdef") is 6, and strlen ("1234 five or six") is 18. This makes it troublesome to check.

The number of Chinese Characters in php is found online, and mb_strlen is used.

I checked the manual:

Int mb_strlen(String str [, string encoding])

Mb_strlen () returns number of characters in stringStrHaving character encodingEncoding. A multi-byte character is counted as 1.

EncodingIs character encodingStr. IfEncodingIs omitted, internal character encoding is used.

Originally, this function is for words and encoded according to different characters. For some characters, the multi-byte is also calculated as 1.

After a try, both mb_strlen ('abcdef', 'utf-8') and mb_strlen ('five or six in 1234, 'utf-8') are equal to 6. OK ~

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.