r/PHPhelp • u/lindymad • Apr 07 '25
Checking if a user-supplied regular expression will only match a number
My situation is as follows:
A user can enter a custom regular expression that validates a field in a form they have created in our system.
I need to know whether that regular expression means that the field validation optionally requires an integer or a decimal. By "optionally" here I mean if the regex accepts blank or an integer or decimal, that would count for my purposes.
The reason is that eventually a temporary database table is constructed and if I know that the only valid values will be integers, I want to make the database field type an INT. If I know that the only valid values will be decimals (or integers), I want to make the database field type a FLOAT. In all other circumstances, the database field type will be TEXT. If the validation allows no value to be entered, it will be a NULL field, if not it will not allow NULL. I know how to check for this already (that's easy - if (preg_match('/'.$sanitizedUserEnteredRegex.'/', '')) // make it a NULL field)
I have no control over what regular expression is entered by a user, so examples of regular expressions that only match an integer could be as simple as /^\d*$/, or as crazy as e.g. /^-?([1-4]+)5\d{1,3}$/. That means I can't just check if a random number happens to match or a random string happens not to match, in the same way I can check for if no value is allowed.
The two things I need help with are:
- How can I determine whether a regular expression will only match an integer.
- How can I determine whether a regular expression will only match an integer or a decimal.
I am aware of the various sanitation requirements of using a user supplied regular expression and it's eventual translation into a database table, I'm not looking for help or advice on that side of things.
Thanks
View all comments
Show parent comment
1
u/lindymad Apr 07 '25
True enough, but it's just part of the process. Get the task, see if it can be done as specified (which if it can, then great), if not then push back with suggestions for how to achieve the same effect in different ways.
I'm currently in the "see if it can be done as specified" phase.