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 ~