How to get parameters from a URL string?

╄→尐↘猪︶ㄣ 提交于 2019-11-25 22:38:12

问题


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.


回答1:


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




回答2:


All the parameters after ? can be accessed using $_GET array. So,

echo $_GET['email'];

will extract the emails from urls.




回答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 = "https://mysite.com/test/1234?email=xyz4@test.com&testin=123";
$query_str = parse_url($url, PHP_URL_QUERY);
parse_str($query_str, $query_params);
print_r($query_params);

//Output: Array ( [email] => xyz4@test.com [testin] => 123 ) 



回答4:


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: http://php.net/manual/en/function.parse-url.php




回答5:


you can use below code to get email address after ? in the URL

<?php
if (isset($_GET['email'])) {
    echo $_GET['email'];
}



回答6:


As mentioned in other answer, best solution is using

parse_url()

You need to use combination of parse_url() and parse_str().

The parse_url() parse URL and return its components that you can get query string using query key. Then you should use parse_str() that parse query string and return values into variable.

$url = "https://example.com/test/1234?basic=2&email=xyz2@test.com";
parse_str(parse_url($url)['query'], $params);
echo $params['email']; // xyz2@test.com

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_match("/&?email=([^&]+)/", $url, $matches);
echo $matches[1]; // xyz2@test.com

preg_replace()

Also you can use preg_replace() to do this work in one line!

$email = preg_replace("/^https?:\/\/.*\?.*email=([^&]+).*$/", "$1", $url);
// xyz2@test.com



回答7:


I created function from @Ruel answer. You can use this:

function get_valueFromStringUrl($url , $parameter_name)
{
    $parts = parse_url($url);
    if(isset($parts['query']))
    {
        parse_str($parts['query'], $query);
        if(isset($query[$parameter_name]))
        {
            return $query[$parameter_name];
        }
        else
        {
            return null;
        }
    }
    else
    {
        return null;
    }
}

Example:

$url = "https://example.com/test/the-page-here/1234?someurl=key&email=xyz5@test.com";
echo get_valueFromStringUrl($url , "email");

Thanks to @Ruel




回答8:


You could get the parameters of the url like this:

email = $_GET["email"];

.. or like this:

$url = $_SERVER["REQUEST_URI"];
$email = str_replace("/path/to/file.php?email=", "", $url);

Examples of the path to file:

if the url looks like this:

https://example.com/file.php

then the path to file is:

/file.php (+ parameter to get. Example: ?email=)



回答9:


$uri = $_SERVER["REQUEST_URI"];
$uriArray = explode('/', $uri);
$page_url = $uriArray[1];
$page_url2 = $uriArray[2];
echo $page_url; <- see the value

This is working great for me using php




回答10:


In Laravel, I'm use:

private function getValueFromString(string $string, string $key)
{
    parse_str(parse_url($string, PHP_URL_QUERY), $result);

    return isset($result[$key]) ? $result[$key] : null;
}



回答11:


$web_url = 'http://www.writephponline.com?name=shubham&email=singh@gmail.com';
$query = parse_url($web_url, PHP_URL_QUERY);
parse_str($query, $queryArray);

echo "Name: " . $queryArray['name'];  // Result: shubham
echo "EMail: " . $queryArray['email']; // Result:singh@gmail.com



回答12:


Dynamic function which parse string url and get value of query parameter passed in URL

 function getParamFromUrl($url,$paramName){
   parse_str(parse_url($url,PHP_URL_QUERY),$op);// fetch query parameters from string and convert to associative array
   return array_key_exists($paramName,$op) ? $op[$paramName] : "Not Found"; // check key is exist in this array
 }

Call Function to get result

 echo getParamFromUrl('https://google.co.in?name=james&surname=bond','surname'); // bond will be o/p here



回答13:


To get parameters from URL string, I used following function.

var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = decodeURIComponent(window.location.search.substring(1)),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
};
var email = getUrlParameter('email');

If there are many URL strings, then you can use loop to get parameter 'email' from all those URL strings and store them in array.



来源:https://stackoverflow.com/questions/11480763/how-to-get-parameters-from-a-url-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!