verify amount of php character [duplicate]

2

How can I check if a string is longer than 3 characters?

Type So:

if ($string >= 3) {
   echo "ok";
} else {
   echo "erro";
}
    
asked by anonymous 28.12.2016 / 13:52

2 answers

3

To count the number of characters in php use the mb_strlen () function. strlen () returns the number of bytes that by default 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);

Output:

strlen: 4
mb_ strlen: 3
    
28.12.2016 / 13:58
2

Use strlen() :

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

if ($contString > 3) {
   echo "Tem mais de 3 Caracteres";
} else {
   echo "Essa string tem $contString caracteres.";
}
    
28.12.2016 / 13:54