I have a HTML form field $_POST["url"]
having some URL strings as the value.
Example values are:
https://example.com/test/1234?email=xyz@test.com https://example.com/test/1234?basic=2&email=xyz2@test.com https://example.com/test/1234?email=xyz3@test.com https://example.com/test/1234?email=xyz4@test.com&testin=123 https://example.com/test/the-page-here/1234?someurl=key&email=xyz5@test.com
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.
Use
$_GET['email']
for parameters in URL. Use$_POST['email']
for posted data to script. Or use_$REQUEST
for both. Also, as mentioned, you can useparse_url()
function that returns all parts of URL. Use a part called 'query' - there you can find your email parameter. More info: http://php.net/manual/en/function.parse-url.phpThis is working great for me using php
Use the parse_url() and parse_str() methods.
parse_url()
will parse a URL string into an associative array of its parts. Since you only want a single part of the URL, you can use a shortcut to return a string value with just the part you want. Next,parse_str()
will create variables for each of the parameters in the query string. I don't like polluting the current context, so providing a second parameter puts all the variables into an associative array.you can use below code to get email address after ? in the URL
You can use the
parse_url()
andparse_str()
for that.If you want to get the
$url
dynamically with PHP, take a look at this question:Get the full URL in PHP
As mentioned in other answer, best solution is using
parse_url()
You need to use combination of
parse_url()
andparse_str()
.The
parse_url()
parse URL and return its components that you can get query string usingquery
key. Then you should useparse_str()
that parse query string and return values into variable.Also you can do this work using regex.
preg_match()
You can use
preg_match()
to get specific value of query string from URL.preg_replace()
Also you can use
preg_replace()
to do this work in one line!