Jump to content

Recommended Posts

If you want to check if the string contains only numbers or not, then use is_numeric() function

<?php
$string="12345";
if(is_numeric($string))
{
echo "String is numeric";
}
?>

 

But this won't work if the string contains more than 1 decimal.

 

 

If you want to use regular expressions, use the below pattern:

$string="123.4.5.";
if(preg_match("/^[0-9.]+$/",$string))
{
echo "String is valid: Numbers and/or decimals";
}
else
{
echo "Invalid";
}

 

Note: I have used preg instead of ereg as I would advise you the same because from PHP 6, ereg would not be bundled with the default PHP package. So ur code might break!

 

btw: is_numeric is faster than preg or ereg.

Link to comment
https://forums.phpfreaks.com/topic/104195-eregi-problems/#findComment-533402
Share on other sites

for eregi

try this (for numbers ONLY)

<?php
#$data = "1233"; //works
$data = "123A"; //fails
if (!eregi('^[[:digit:]]+$', $data))
{
echo "Number ONLY Required";
}
?>

 

But Rohan Shenoy is correct, don't use a regex if you can do it another way if(is_numeric($string)) is quicker

Link to comment
https://forums.phpfreaks.com/topic/104195-eregi-problems/#findComment-533405
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.