sandelius
sandelius

Reputation: 523

Regular Expression for $_GET query strings

I'm trying to find a regular expression for $_GET query strings.

I have an array like this:

private $_regexp = array(
    ':id'    => '[0-9]+',
    ':year'  => '[12][0-9]{3}',
    ':month' => '0[1-9]|1[012]',
    ':day'   => '0[1-9]|[12][0-9]|3[01]',
    ':slug'  => '[a-zA-Z0-9-]+',
    ':query' => '...'
);

and I loop throw them to see if I have a matching wildcard like this:

if ( array_key_exists($matches[0], $this->_regexp) )
    {
        return '^('.$this->_regexp[$matches[0]].')$';
    }

All other regexp go throw but I've tried a whole lot of different regexp to find:

?anything=anything

can't figure it out, googled like h..l but can't find anything. I've tried, for example something like this:

(\?)(.*)(=)(.*)

but without result...

Any regexp gurus here?

/ Tobias

Upvotes: 0

Views: 373

Answers (2)

Rik Heywood
Rik Heywood

Reputation: 13972

How about:-

(\?)([^=]+)(=)(.+)

Upvotes: 0

Tomalak
Tomalak

Reputation: 338228

Though I don't really understand the question, your regex would be

\?([^=]+)=([^&]*)
\?         # a literal question mark
(          # group 1
  [^=]+    # anything but a "=", 1-unlimited chars
)          # end group 1
=          # the "="
(          # group 2
  [^&]*    # anything but a "&", 0-unlimited chars
)          # end group 2

Can you explain what you are actually trying to do?

Upvotes: 1

Related Questions