Xerri
Xerri

Reputation: 5046

regex to get $_GET variables

I have a URL string and would like to extract parts of the URL. I have been trying to do understand how to do it with regex but no luck.

http://www.example.com?id=example.id&v=other.variable

From the example above I would like to extract the id value ie. example.id

Upvotes: 2

Views: 725

Answers (3)

delphist
delphist

Reputation: 4539

$url = 'http://www.example.com?id=example.id&v=other.variable';
parse_str(parse_url($url, PHP_URL_QUERY), $vars);

Upvotes: 2

Mārtiņš Briedis
Mārtiņš Briedis

Reputation: 17762

No need for regexp here, just use php built in function parse_url

Upvotes: 4

Pekka
Pekka

Reputation: 449495

I'm assuming you're not referring to actual $_GET variables, but to a string containing a URL with a query string.

PHP has built-in functions to process those:

  • parse_url() to extract the query string from a URL
  • parse_str() to split the query string into its components

Upvotes: 6

Related Questions