PHP 

This is an example on how to verify a string only contains integer values. String may contain anything like float value, double value, characters etc. This example uses regular expression to check a string contains only int values.

Below is the function which validates a string contains only int values.

function string_contains_int($str) {
    return preg_match("/^[1-9][0-9]*$/", $str);
}

Usage Examples

$str = "6546545987";
$string_contains_int = string_contains_int($str);
echo ($string_contains_int == 1 ? "true" : "false");
Output: true
$str = "65465459gg87";
$string_contains_int = string_contains_int($str);
echo ($string_contains_int == 1 ? "true" : "false");
Output: false
$str = "65467.50";
$string_contains_int = string_contains_int($str);
echo ($string_contains_int == 1 ? "true" : "false");
Output: false

Thanks for your reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on JEE Tutorials | TwitterFacebook Google PlusLinkedin | Reddit | Email Me

Leave a Reply

Your email address will not be published. Required fields are marked *