PHP Regex on Query Strings
Posted: Thu Sep 10, 2009 6:52 pm
I'm having trouble understanding my own code. I understand what I wrote, of course. But, the output is frustrating me.
I'd figured that, from my understanding on the the regular-expressions.info website, conditionals is not included in the php version of regex. So, with that, I figured I would have to write some conditions to match the query string variable to it's corresponding value through using the array parameter in preg_match.
But, it turns out that that's not even necessary in the way I wrote my regex string. It seems php formulates the conditions in the regex already! I mean, the entry qstring var must only contain digit(s), filename must be a valid text file name, and page can only have the values add, edit or main.
Why is the regex doing this?
I'm using PHP Windows 5.2.8 in IIS 5
Here's an example code:
I'd figured that, from my understanding on the the regular-expressions.info website, conditionals is not included in the php version of regex. So, with that, I figured I would have to write some conditions to match the query string variable to it's corresponding value through using the array parameter in preg_match.
But, it turns out that that's not even necessary in the way I wrote my regex string. It seems php formulates the conditions in the regex already! I mean, the entry qstring var must only contain digit(s), filename must be a valid text file name, and page can only have the values add, edit or main.
Why is the regex doing this?
I'm using PHP Windows 5.2.8 in IIS 5
Here's an example code:
Code: Select all
// qstring( $_SERVER['QUERY_STRING'] );
function qstring_db( $qstring )
{
foreach( split('&', $qstring) as $qentry )
{
// matches[1] = qentry var name
// matches[2] = entry
// matches[3] = filename
// matches[4] = page
// matches[5] = qentry value
// matches[6] = entry value
// matches[7] = filename value
// matches[8] = page value
if( preg_match('/^((entry)|(filename)|(page))'
.'=(([0-9]+)|([^><\*\?"\r\n\|\/\\\\]+\.txt)|(add|edit|main))$/i'
, $qentry, $matches) )
{ print_array( $matches ); }
}
}