3

I'm looking for a method that would allow me to check for valid IDs in my system.

They're by definition:

  • positive, not 0,
  • a whole number, but might be bigger than integer size
  • come as either string or number

I'd like to know what you think is the most elegant way of validating this. Candidates that fail are:

  • is_int(), because it's valid for negative numbers, AND because of the integer size limit
  • is_numeric(), because it'd valid for float, or, say, having an exponential part like this +0123.45e6
  • ctype_digit() with casting the input to string, because this would be valid: "0000"

I've so far settled with this:

preg_match('/^[1-9][0-9]*$/',(string)$id);

Can anyone come up with an alternative?

3
  • possible duplicate of stackoverflow.com/questions/1969464/… Commented Jan 16, 2012 at 11:06
  • not quite: the real kicker is the "bigger than int" while still a whole number. It could apply to the person asking in the other thread, though, if his tables get real big. Commented Jan 16, 2012 at 12:22
  • Note: I'm aware that creating a function, combining various methods, is a possibility (which I'm using in practice, I don't expose the regexp every time), I'm wondering if PHP is offering me something more elegant. Commented Jan 16, 2012 at 12:24

2 Answers 2

3

Try filter_var functions with FILTER_VALIDATE_INT. For valid integers you can specify ranges.Probably has the same limitaton with the size limit, but for that you'll have to build a custom function that serve exactly your requirements.

Sign up to request clarification or add additional context in comments.

4 Comments

I guess I could wrap it in filter var, like:
I guess I could wrap it in filter_var, like: filter_var((string) $id, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '/^[1-9][0-9]*$/')) ); probably a good idea when that's a project-wide validation approach (which it isn't right now)
my real question is why are you using id with INT over the INT limits? this will turn against you at some point. Use another combination of key if you need more keys
The definition of the id is outside of my control. That said, I'm not "using id with INT" over its limits; it's not INT at all, just a whole number as a string. I wildly guess it has something to do with id pools and distributed servers and stuff. Mainly with stuff, though.
0

you could use

function vaild_id($passed_value=-1)
{
    if((is_int($passed_value)) && ($passed_value > 0)) return true;
    else return false; 
}

this will also allow you to add other rules to it if you need to

3 Comments

And what about integer size limit: is_int( 2147483648 );// false?
added note about is_int size limit
yes, it's of no use if it limits me to the size of int, be that 32 or 64 bit environments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.