EchoFool Posted November 26, 2008 Share Posted November 26, 2008 I am not sure why but my preg match fails and keeps setting quantity to zero every time. Im trying to get it to only allow full integers only. Examples: 1 - correct 1.5 - not correct +1 - not correct -1 - not correct f1 - not correct etc Why does it keep failing ? <?php $Quantity = 2; If(!(preg_match('~^\d\+\.\-$~', $Quantity))){ $Quantity = 0; } Echo $Quantity; ?> Link to comment https://forums.phpfreaks.com/topic/134291-solved-preg-match-not-working/ Share on other sites More sharing options...
Philip Posted November 26, 2008 Share Posted November 26, 2008 Why not just use is_int? http://us3.php.net/manual/en/function.is-int.php <?php $quantity = 2; if(is_int($quantity) && $quantity>0) { $quantity = 0; } ?> Link to comment https://forums.phpfreaks.com/topic/134291-solved-preg-match-not-working/#findComment-699122 Share on other sites More sharing options...
DeanWhitehouse Posted November 26, 2008 Share Posted November 26, 2008 or is_numeric();? http://uk2.php.net/is_numeric Link to comment https://forums.phpfreaks.com/topic/134291-solved-preg-match-not-working/#findComment-699123 Share on other sites More sharing options...
EchoFool Posted November 26, 2008 Author Share Posted November 26, 2008 Because wouldn't +9999 be true for is_int ? And is_numeric would accept things like 1.5 ? Link to comment https://forums.phpfreaks.com/topic/134291-solved-preg-match-not-working/#findComment-699136 Share on other sites More sharing options...
DeanWhitehouse Posted November 26, 2008 Share Posted November 26, 2008 I don't think they do, try them out ? Read the comments on the is_numeric page From the comments <?php function my_is_numeric($value) { $american = preg_match ("/^(-){0,1}([0-9]+)(,[0-9][0-9][0-9])*([.][0-9]){0,1}([0-9]*)$/" ,$value) == 1; $world = preg_match ("/^(-){0,1}([0-9]+)(.[0-9][0-9][0-9])*([,][0-9]){0,1}([0-9]*)$/" ,$value) == 1; return ($american or $world); } ?> Link to comment https://forums.phpfreaks.com/topic/134291-solved-preg-match-not-working/#findComment-699137 Share on other sites More sharing options...
btherl Posted November 26, 2008 Share Posted November 26, 2008 EchoFool also wants "-1" to fail, but "-1" is numeric by that regexp. I think it should just be this: preg_match('~^\d+$~', $n); Link to comment https://forums.phpfreaks.com/topic/134291-solved-preg-match-not-working/#findComment-699147 Share on other sites More sharing options...
Philip Posted November 26, 2008 Share Posted November 26, 2008 Sorry, I just realized I overlooked some of my mistakes in my first reply, which now is tested and works in all cases (well, almost... not "+1 - not correct", how you see that is really different from "1" I'm not sure.): <?php if(!is_int($quantity) || $quantity<0) { $quantity = 0; } ?>] However, btherl, I ran your preg with the test cases and they failed. Still allows the decimals/negatives Link to comment https://forums.phpfreaks.com/topic/134291-solved-preg-match-not-working/#findComment-699151 Share on other sites More sharing options...
EchoFool Posted November 26, 2008 Author Share Posted November 26, 2008 Sorry, I just realized I overlooked some of my mistakes in my first reply, which now is tested and works in all cases (well, almost... not "+1 - not correct", how you see that is really different from "1" I'm not sure.): <?php if(!is_int($quantity) || $quantity<0) { $quantity = 0; } ?>] However, btherl, I ran your preg with the test cases and they failed. Still allows the decimals/negatives I was concerned that something like 1+2 would be accepted thus also allowing things like 1*4 etc which would change the value that went in. But it is working now so thank you Works a tread. Link to comment https://forums.phpfreaks.com/topic/134291-solved-preg-match-not-working/#findComment-699154 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.