I’m trying to validate string to check is there any numeric value.. but the condition is not giving me expected result. What’s the problem ?
$data = 'String';
$splt_data = str_split($data);
print_r($splt_data);
for ($i = 0; $i < count($splt_data); $i++) {
for ($x = 0; $x < 10; $x++) {
if ($splt_data[$i] == $x) {
echo "<br> The \"$data\" value is containing numeric value.";
echo "<br>" . $splt_data[$i] . ' and ' . $x;
exit();
}
}
}
>Solution :
If you don’t care about splitting the string and comparing each character, you could use a regex to see if there are any digits in the string:
$data = "String";
if(preg_match("/\\d/", $data)){
// contains a digit
}
