sorenchr Posted May 5, 2008 Share Posted May 5, 2008 Hi, im new to the ereg & eregi functions, so after trying hard for 2 hours i gave up. Im trying to create an eregi function, that gives an error, if the target string has anything else but numbers in it. Is this even possible to create? Quote Link to comment https://forums.phpfreaks.com/topic/104195-eregi-problems/ Share on other sites More sharing options...
Rohan Shenoy Posted May 5, 2008 Share Posted May 5, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/104195-eregi-problems/#findComment-533402 Share on other sites More sharing options...
MadTechie Posted May 5, 2008 Share Posted May 5, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/104195-eregi-problems/#findComment-533405 Share on other sites More sharing options...
sorenchr Posted May 5, 2008 Author Share Posted May 5, 2008 Thanks!! Works like a charm Quote Link to comment https://forums.phpfreaks.com/topic/104195-eregi-problems/#findComment-533559 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.