Jump to content

[SOLVED] only numbers


dadamssg

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/148405-solved-only-numbers/
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/148405-solved-only-numbers/#findComment-779333
Share on other sites

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]+~

Link to comment
https://forums.phpfreaks.com/topic/148405-solved-only-numbers/#findComment-779697
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/148405-solved-only-numbers/#findComment-779709
Share on other sites

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!).

Link to comment
https://forums.phpfreaks.com/topic/148405-solved-only-numbers/#findComment-779717
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.