php determines that the input does not exceed the length range of the varchar field of mysql

  • 2020-05-07 19:23:28
  • OfStack

However, under the utf-8 encoding, a Chinese character is 3 characters long, such as the string $str= "hello!!" ;

If you use the strlen function, the length is 11, which is just over the length of varchar, but it's not. If you go directly to phpmyadmin and execute insert, this string can be inserted!

For the database, it has a length of 5, so how do we use PHP to get this length? Use the iconv_strlen() function!

utf echo iconv_strlen ($str, '-');

Note that the second parameter is the current character set, so that depending on the character set you get the result is calculated according to the length of one character!

The above statement will output 5. How about that? Can you judge?

Related articles: