PHP strlen and mb_strlen not working as expected

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

PHP functions strlen() and mb_strlen() both are returning the wrong number of characters when I run them on a string.

Here is a piece of the code I'm using...

 $foo = mb_strlen($itemDetails['ITEMDESC'], 'UTF-8');  echo $foo; 

It also tells me that this string - "Infant Heel Warmer, No Adhesive Attachment Pad, 100/cs" is 54, which is correct.

I assume its some issue with character encoding, everything should be UTF-8 I think. I've tried feeding mb_strlen() several different character encoding types and they all are returning this oddball count with the string that has those non-standard characters.

I've no idea why this is happening.

回答1:

Did the text originate from an HTML form? Ensure your <form> element includes the accept-charset="UTF-8" attribute.

Did the text get stored in a database along the way? Make sure the database stores and returns the data in UTF-8. This means checking the server's global defaults, the defaults for the database or schema, and the table itself.



回答2:

It is very likely that your input is encoded in UTF-16. You may convert to UTF-8

$foo = mb_strlen(mb_convert_encoding($itemDetails['ITEMDESC'], "UTF-8", "UTF-16"));

or if you use mb_strlen() be sure to use proper encoding as a second parameter.

$foo = mb_strlen($itemDetails['ITEMDESC'], "UTF-16");

Without correct encoding mb_strlen will always return wrong results. It's easy to get into troubles when you're dealing with UTF-8/16/32 encoded strings. mb_detect_encoding() will not solve this problem.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!