开发者

mb_strlen() is it enough?

开发者 https://www.devze.com 2023-01-14 04:42 出处:网络
When counting the length of an UTF-8 string in PHP I use mb_strlen(). For example: if (mb_strlen($name, \'UTF-8\') < 3) {

When counting the length of an UTF-8 string in PHP I use mb_strlen().

For example:

if (mb_strlen($name, 'UTF-8') < 3) {
    $error .= 'Name is required. Minimum of 3 characters required in name.';
}

As the text fields can accept any language (multilanguage), I want to make sure that PHP will count mutltilanguage UTF-8 characters correctly.

Is this sufficient or do I need to use other methods? I am concerned PHP may get it wrong for some r开发者_JS百科eason, or maybe I am just being skeptical, but I don't want people bypassing important validation because PHP is getting it wrong.


Correct

if (mb_strlen($name, 'UTF-8') < 3) is sufficient enough

make sure header is correct

HTTP-header (Content-Type: text/html; charset=UTF-8)

you can also check alternative for some reason

strlen(utf8_decode($string))

UTF-8 to Code Point Array Converter in PHP


In addition to JapanPro's answer:
Your HTML can have :

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

instead of an implicit header declaration:

HTTP-header (Content-Type: text/html; charset=UTF-8)
0

精彩评论

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