-4

If, for example, a person wants their username to be GreenApples-72, how do you count the number of letters, numbers and symbols in the username and add them up?

        if((($_POST['username'])<'2'))    //if the username length is less than 2.
        {
            echo('Your username has fewer than 2 characters');
        }

This is my code so far, this is the checking stage of the code. I'm not sure on how to code this part.

3 Answers3

0

Use strlen:

echo strlen("GreenApples-72"); // echoes 14
Ende Neu
  • 15,581
  • 5
  • 57
  • 68
0

Use strlen() function in php to count the length of the variable. Use the code below to count the length of the variable

if(strlen($_POST['username'])<'2')    //if the username length is less than 2.
        {
            echo('Your username has fewer than 2 characters');
        }

Hope this helps you

Utkarsh Dixit
  • 4,267
  • 3
  • 15
  • 38
0

If you allow all characters at this stage (and it seems you do), you should use multibyte function mb_strlen() to check string length:

 mb_internal_encoding('UTF-8'); // set utf-8 encoding for all mb_ functions

 if(mb_strlen($_POST['username'])<'2')    //if the username length is less than 2.
 {
    echo('Your username has fewer than 2 characters');
 }

strlen won't be good enough in this case because:

echo strlen('ą'); // will return 2

echo mb_strlen('ą','UTF-8'); // will return 1
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291