How to get parameters from a URL string?

2018-12-31 08:11发布

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.

10条回答
永恒的永恒
2楼-- · 2018-12-31 08:47

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

查看更多
流年柔荑漫光年
3楼-- · 2018-12-31 08:50

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

echo $_GET['email'];

will extract the emails from urls.

查看更多
看风景的人
4楼-- · 2018-12-31 08:51

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.

查看更多
后来的你喜欢了谁
5楼-- · 2018-12-31 08:52

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=)
查看更多
登录 后发表回答