How to find comma delimited value is string or number



Is there a way to check for integers when there are commas in the value?

Yes

you can simply strip out the commas by treating the input as a string:

$value1 = str_replace(',', '', $value1);

Then you can check for integers using a regular expression:


if (preg_match('/^-?\d+$/', $value1)) {

    ...

}

Or, if decimals are allowed, you can use is_numeric()


if (is_numeric($value1)) {

    ...

}

// my code to solve the issue

$d = "1,2,3,4,5,6"; 

$value1 = str_replace(',', '', $d);

if (preg_match('/^-?\d+$/', $value1)) { 

echo "it is number only";

}

else

{

echo "no number found";

}

Comments