Hi friends,
I have following string.
?page=1&sort=desc¶m=5&parm2=25
I need to check whether the enter string url is in strict format except variable value. Like page=, sort=, param=, param2.
Please suggest regular expression. Thanks
Hi friends,
I have following string.
?page=1&sort=desc¶m=5&parm2=25
I need to check whether the enter string url is in strict format except variable value. Like page=, sort=, param=, param2.
Please suggest regular expression. Thanks
Hi,
You should use parse_str and check if all the parameters you wanted are set with isset
. Regex is not the way to go here.
If you care about the order of parameters, something like this:
\?page=[^&]+&sort=[^&]+param=[^&]+param2=[^&]+$
But Alin Purcaru is right - use the parse_str function already written to do this
You could use the following regx /\?page=[^&]*sort=[^&]*param=[^&]*param2=/` to match:
if (preg_match("/\?page=([^&]*)sort=([^&]*)param=([^&]*)param2=([^&]*)/i", $inputstr, $matches))
{
echo "Matches:";
print_r($matches); // matches will contain the params
}
else
echo "Params nor found, or in wrong order;
Maybe this :
\?page=\d+&sort=.+¶m=\d+¶m2=\d+
which translates to :
?page= followed by any digit repeated 1 or more times
&sort= followed by any character repeated 1 or more times
¶m= followed by any digit repeated 1 or more times
¶m2= followed by any digit repeated 1 or more times
I think Alin Purcaru 's suggestion is better
EDIT:
(\?|&)(page=[^&]+|sort=[^&]+|param=[^&]+|parm2=[^&]+)
This way the order doesn't matter
The regex would be ^\?([\w\d]+=[\w\d]+(|&))*$
As long as your values are Alpha Numeric, but maybe you wanna take a look in to filters if you want to validate an url http://www.php.net/manual/en/book.filter.php