Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Validating numeric values in a string. Why is this condition giving true?

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();
        }
    }
}

Output

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading