check amount of php character

Asked

Viewed 3,232 times

3

How can I check if a string has more than 3 characters?

Type Thus:

if ($string >= 3) {
   echo "ok";
} else {
   echo "erro";
}
  • vlw thank you so much.

2 answers

5

To count the number of characters in php use the function mb_strlen(). strlen() returns the number of bytes that by knowing can return the same number of characters.

Excerpt from the documentation:

strlen() Returns the number of bytes rather than the number of characters in a string.

$str = 'NÃO';

echo 'strlen: '. strlen($str) .'<br>';
echo 'mb_ strlen: '. mb_strlen($str);

Exit:

strlen: 4
mb_ strlen: 3
  • vlw thank you so much.

2

Use the strlen():

$string = "Teste de Contador";
$contString = strlen($string);

if ($contString > 3) {
   echo "Tem mais de 3 Caracteres";
} else {
   echo "Essa string tem $contString caracteres.";
}
  • vlw thank you so much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.