Reputation: 5046
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
Reputation: 4539
$url = 'http://www.example.com?id=example.id&v=other.variable';
parse_str(parse_url($url, PHP_URL_QUERY), $vars);
Upvotes: 2
Reputation: 17762
No need for regexp here, just use php built in function parse_url
Upvotes: 4
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 URLparse_str()
to split the query string into its componentsUpvotes: 6