Use preg_match()
:
if (preg_match('~[0-9]+~', $string)) {
echo 'string with numbers';
}
Althought you should not use it, as it is much slower than preg_match()
I will explain why your original code is not working:
A non numerical character in the string when compared to a number (in_array()
does that internally) would be evaluated as 0
what is a number. Check this example:
var_dump('A' == 0); // -> bool(true)
var_dump(in_array('A', array(0)); // -> bool(true)
Correct would be to use is_numeric()
here:
$keyword = 'doesn\'t include';
for ($i = 0; $i <= strlen($string)-1; $i++) {
if(is_numeric($string[$i])) {
$keyword = 'includes';
break;
}
}
Or use the string representations of the numbers:
$keyword = 'doesn\'t include';
// the numbers as stings
$numbers = array('0', '1', '2', /* ..., */ '9');
for ($i = 0; $i <= strlen($string)-1; $i++) {
if(in_array($string[$i], $numbers)){
$keyword = 'includes';
break;
}
}