How can I check if a string is longer than 3 characters?
Type So:
if ($string >= 3) {
echo "ok";
} else {
echo "erro";
}
How can I check if a string is longer than 3 characters?
Type So:
if ($string >= 3) {
echo "ok";
} else {
echo "erro";
}
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
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.";
}