Jump to content

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

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

}

Link to comment
https://forums.phpfreaks.com/topic/148405-solved-only-numbers/#findComment-779324
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

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.