开发者

Counting number of Unicode characters for MySQL database

开发者 https://www.devze.com 2023-04-01 19:06 出处:网络
When, in PHP, I have variable that\'s hold Unicode char开发者_开发知识库acters do I need to worry about storing it in MySQL database?

When, in PHP, I have variable that's hold Unicode char开发者_开发知识库acters do I need to worry about storing it in MySQL database?

Lets say I have variable value Dejan Čolić, I suppose that will for Unicode characters use 2 chars (or 4, I am not sure). Then my variable will be 13/17 chars long in database instead of 11 chars that are firstly specified. Am I right?

If I am right, is there some PHP function that will help count future Unicode characters so I could do proper data validation? Thanks.


PHP: mb_strlen()
MySQL: CHAR_LENGTH()


Characters are characters. 11 characters are 11 characters regardless of the environment. If MySQL doesn't know you are giving it UTF-8 encoded data, it may misinterpret the bytes and derive a different character count based on the assumption of a wrong encoding. But if MySQL is made aware of the correct encoding, it'll be 11 characters.

It'll be more bytes than that though, and even more bytes in MySQL since MySQL is storing UTF-8 data rather inefficiently.

0

精彩评论

暂无评论...
验证码 换一张
取 消