dadamssg Posted March 7, 2009 Share Posted March 7, 2009 im using a variable passed the url as in mysite.com/work.php?id=45. I $_GET that number and use it in query in my database. I just need a simple pattern that checks whether its numerical or not. I don't want someone typing in sql in the url, refreshing and then a table is dropped or something. If the url is not a number, ill send them to the homepage or something. thanks, any advice would be awesome! Quote Link to comment https://forums.phpfreaks.com/topic/148405-solved-only-numbers/ Share on other sites More sharing options...
corbin Posted March 8, 2009 Share Posted March 8, 2009 http://php.net/ctype_digit (Note that with PHP < 5.1 it accepted empty strings as numeric.) But, if you really want to go the regexp route (which there is no need for): if(preg_match('~[\d]+~', $something)) { //$something consists of 1 or more digits } Quote Link to comment https://forums.phpfreaks.com/topic/148405-solved-only-numbers/#findComment-779324 Share on other sites More sharing options...
nrg_alpha Posted March 8, 2009 Share Posted March 8, 2009 corbin, I definitely agree with you on using ctype_digit on this one. Note however that your pattern (should the OP go this route [and again, I personally wouldn't, not for stuff like this]) doesn't need the character class square brackets, as \d is already a short-hand character class for digits in its own right. Quote Link to comment https://forums.phpfreaks.com/topic/148405-solved-only-numbers/#findComment-779333 Share on other sites More sharing options...
dadamssg Posted March 8, 2009 Author Share Posted March 8, 2009 perfect, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/148405-solved-only-numbers/#findComment-779343 Share on other sites More sharing options...
corbin Posted March 8, 2009 Share Posted March 8, 2009 Hrmmm, guess I've been having a slow moment for a long time. I thought shorthand character classes were only valid in []. I learn new things every day ;p. Quote Link to comment https://forums.phpfreaks.com/topic/148405-solved-only-numbers/#findComment-779344 Share on other sites More sharing options...
nrg_alpha Posted March 8, 2009 Share Posted March 8, 2009 I thought shorthand character classes were only valid in []. No, stuff like \d, \w etc... function just the same whether they are inside or outside a character class. Since \d matches exponents as well, and say you wanted only 0-9, then you would need to encase 0-9 within a character class, as 0-9 alone is not a short-hand character class of any sort. ~[0-9]+~ The only time (that I can think of at the moment) where you would need to wrap character class brackets around those is if: a) you want to check for additional stuff, like a string consisting of only \d or - characters: ~[\d-]+~ // I'm ignoring ^, $, \A and \z stuff for 'expression simplicity' sake. b) you want to match / capture anything that is not listed (thus using a negated character class): ~[^\d]+~ Quote Link to comment https://forums.phpfreaks.com/topic/148405-solved-only-numbers/#findComment-779697 Share on other sites More sharing options...
corbin Posted March 8, 2009 Share Posted March 8, 2009 Wow I just realized that my regexp earlier, not only was it bad, but it was entirely wrong ;p. It would have needed ^ and $. Hrmmm I had always thought \d was 0-9. Guess it makes sense that it's other stuff too though. I swear I'm not regular expressionally retarded.... Just slow ;p. Quote Link to comment https://forums.phpfreaks.com/topic/148405-solved-only-numbers/#findComment-779709 Share on other sites More sharing options...
nrg_alpha Posted March 8, 2009 Share Posted March 8, 2009 Hrmmm I had always thought \d was 0-9 Yeah, this is the common belief. Granted, in most cases, \d will in fact do just fine.. (as how often does one actually run into exponents?). But it's a nice thing to know in the event exponents do find their way into strings. I swear I'm not regular expressionally retarded.... Just slow ;p. lol, I know you not. Not even slow..we all have our 'off days', and god knows, I've had more than my fair share (with more to come I'm sure!). Quote Link to comment https://forums.phpfreaks.com/topic/148405-solved-only-numbers/#findComment-779717 Share on other sites More sharing options...
corbin Posted March 8, 2009 Share Posted March 8, 2009 Hrmmmm wonder how the OP's query would've responded to something like 10^5 lol. Quote Link to comment https://forums.phpfreaks.com/topic/148405-solved-only-numbers/#findComment-779723 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.