How to get parameters from a URL string?

Asim Zaidi picture Asim Zaidi · Jul 14, 2012 · Viewed 546.1k times · Source

I have a HTML form field $_POST["url"] having some URL strings as the value. Example values are:

https://example.com/test/[email protected]
https://example.com/test/1234?basic=2&email;[email protected]
https://example.com/test/[email protected]
https://example.com/test/[email protected]&testin;=123
https://example.com/test/the-page-here/1234?someurl=key&email;[email protected]

etc.

How can I get only the email parameter from these URLs/values?

Please note that I am not getting these strings from browser address bar.

Answer

Ruel picture Ruel · Jul 14, 2012

You can use the parse_url() and parse_str() for that.

$parts = parse_url($url);
parse_str($parts['query'], $query);
echo $query['email'];

If you want to get the $url dynamically with PHP, take a look at this question:

Get the full URL in PHP