I have an HTML form field $_POST["url"]
, having some URL strings as the value.
Example values are:
//example.com/test/1234?email= //example.com/test/1234?basic=2&email= //example.com/test/1234?email= //example.com/test/1234?email=&testin=123 //example.com/test/the-page-here/1234?someurl=key&email=
etc.
How can I get only the email
parameter from these URLs/values?
Please note that I am not getting these strings from the browser address bar.
asked Jul 14, 2012 at 3:27
Asim ZaidiAsim Zaidi
25.8k48 gold badges129 silver badges219 bronze badges
5
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
answered Jul 14, 2012 at 3:47
2
All the parameters after ?
can be accessed using $_GET
array. So,
echo $_GET['email'];
will extract the emails from urls.
answered Jul 14, 2012 at 3:29
hjpotter92hjpotter92
76.2k34 gold badges137 silver badges176 bronze badges
3
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.
$url = "//mysite.com/test/1234?email=&testin=123";
$query_str = parse_url[$url, PHP_URL_QUERY];
parse_str[$query_str, $query_params];
print_r[$query_params];
//Output: Array [ [email] => [testin] => 123 ]
answered Jul 14, 2012 at 4:01
JCottonJCotton
11.5k5 gold badges53 silver badges57 bronze badges
As mentioned in another answer, the best solution is using parse_url[]
.
You need to use a combination of parse_url[]
and parse_str[]
.
The parse_url[]
parses the URL and return its components
that you can get the query string using the query
key. Then you should use parse_str[]
that parses the query string and returns values into a variable.
$url = "//example.com/test/1234?basic=2&email=";
parse_str[parse_url[$url]['query'], $params];
echo $params['email']; //
Also you can do this work using regex: preg_match[]
You can use preg_match[]
to get a specific value of the query string from a URL.
preg_match["/&?email=[[^&]+]/", $url, $matches];
echo $matches[1]; //
preg_replace[]
Also you can use preg_replace[]
to do this work in one line!
$email = preg_replace["/^https?:\/\/.*\?.*email=[[^&]+].*$/", "$1", $url];
//
answered Nov 11, 2018 at 9:49
MohammadMohammad
20.5k15 gold badges53 silver badges80 bronze badges
1
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 use parse_url[]
function that returns all parts of URL. Use a part called 'query' - there you can find your email parameter. More info: //php.net/manual/en/function.parse-url.php
answered Jul 14, 2012 at 3:56
1
You can use the below code to get the email address after ?
in the URL: