8

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 reason, or maybe I am just being skeptical, but I don't want people bypassing important validation because PHP is getting it wrong.

Barm
  • 403
  • 5
  • 11
PHPLOVER
  • 7,047
  • 18
  • 37
  • 54

2 Answers2

22

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

Pramendra Gupta
  • 14,667
  • 4
  • 33
  • 34
  • Thanks JapanPro. My HTTP-header is fine thank you. Will check that link out. So i guess mb_strlen() can be trusted then as my http header is as you have said above? Thanks! – PHPLOVER Sep 04 '10 at 04:42
  • I just wanted to point out that in Dutch there are names with 2 characters: An and Japanese names can also have only 2 chararcters. 林田 (hayashida) for example. – anatak Jun 04 '20 at 00:28
1

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)
Nasser Al-Wohaibi
  • 4,562
  • 2
  • 36
  • 28