php 如何从 URL 字符串中获取参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11480763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:36:35  来源:igfitidea点击:

How to get parameters from a URL string?

phpurl-parsing

提问by Asim Zaidi

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

我有一个 HTML 表单字段$_POST["url"],其中包含一些 URL 字符串作为值。示例值为:

https://example.com/test/[email protected]
https://example.com/test/1234?basic=2&[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 protected]

etc.

等等。

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

如何email仅从这些 URL/值中获取参数?

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

请注意,我没有从浏览器地址栏中获取这些字符串。

回答by Ruel

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

为此,您可以使用parse_url()parse_str()

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

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

如果你想$url用PHP动态获取,看看这个问题:

Get the full URL in PHP

获取 PHP 中的完整 URL

回答by hjpotter92

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

之后的所有参数?都可以使用$_GET数组访问。所以,

echo $_GET['email'];

will extract the emails from urls.

将从网址中提取电子邮件。

回答by JCotton

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.

使用parse_url()parse_str()方法。parse_url()将 URL 字符串解析为它的部分的关联数组。由于您只需要 URL 的一个部分,因此您可以使用快捷方式返回一个只包含您想要的部分的字符串值。接下来,parse_str()将为查询字符串中的每个参数创建变量。我不喜欢污染当前上下文,因此提供第二个参数会将所有变量放入关联数组中。

$url = "https://mysite.com/test/[email protected]&testin=123";
$query_str = parse_url($url, PHP_URL_QUERY);
parse_str($query_str, $query_params);
print_r($query_params);

//Output: Array ( [email] => [email protected] [testin] => 123 ) 

回答by Paul Denisevich

Use $_GET['email']for parameters in URL. Use $_POST['email']for posted data to script. Or use _$REQUESTfor 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

使用$_GET['email']在URL参数。使用$_POST['email']用于发布的数据脚本。或_$REQUEST两者兼用。此外,如前所述,您可以使用parse_url()返回 URL 的所有部分的函数。使用名为“查询”的部分 - 在那里您可以找到您的电子邮件参数。更多信息:http: //php.net/manual/en/function.parse-url.php

回答by Gulshan kumar

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

您可以使用以下代码在之后获取电子邮件地址吗?在网址中

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

回答by Mohammad

As mentioned in other answer, best solution is using

正如其他答案中提到的,最好的解决方案是使用

parse_url()

parse_url()

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

您需要使用parse_url()和 的组合parse_str()

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

parse_url()解析URL,返回其组成,你可以用得到的查询字符串query键。然后您应该使用parse_str()该解析查询字符串并将值返回到变量中。

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


Also you can do this work using regex.

您也可以使用regex完成这项工作。

preg_match()

preg_match()

You can use preg_match()to get specific value of query string from URL.

您可以使用preg_match()从 URL 获取查询字符串的特定值。

preg_match("/&?email=([^&]+)/", $url, $matches);
echo $matches[1]; // [email protected]

preg_replace()

preg_replace()

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

您也可以使用一行preg_replace()来完成这项工作

$email = preg_replace("/^https?:\/\/.*\?.*email=([^&]+).*$/", "", $url);
// [email protected]

回答by mghhgm

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

从 @Ruel answer创建了函数。你可以使用这个:

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 protected]";
echo get_valueFromStringUrl($url , "email");

Thanks to @Ruel

感谢@Ruel

回答by Asesha George

$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

这对我使用 php 很有用

回答by SHUBHAM SINGH

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

echo "Name: " . $queryArray['name'];  // Result: shubham
echo "EMail: " . $queryArray['email']; // Result:[email protected]

回答by squarecandy

A much more secure answer that I'm surprised is not mentioned here yet:

这里还没有提到一个让我感到惊讶的更安全的答案:

filter_input

filter_input

So in the case of the question you can use this to get an email value from the URL get parameters:

因此,在问题的情况下,您可以使用它从 URL 获取参数中获取电子邮件值:

$email = filter_input( INPUT_GET, 'email', FILTER_SANITIZE_EMAIL );

$email = filter_input( INPUT_GET, 'email', FILTER_SANITIZE_EMAIL );

For other types of variables, you would want to choose a different/appropriate filtersuch as FILTER_SANITIZE_STRING.

对于其他类型的变量,您可能需要选择不同/适当的过滤器,例如FILTER_SANITIZE_STRING.

I suppose this answer does more than exactly what the question asks for - getting the raw data from the URL parameter. But this is a one-line shortcut that is the same result as this:

我想这个答案不仅仅是问题所要求的 - 从 URL 参数获取原始数据。但这是一个与此结果相同的单行快捷方式:

$email = $_GET['email'];
$email = filter_var( $email, FILTER_SANITIZE_EMAIL );

Might as well get into the habit of grabbing variables this way.

不妨养成以这种方式抓取变量的习惯。