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

时间:2022-08-24 12:20:46

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/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?

如何只从这些url /值获取电子邮件参数?

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

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

9 个解决方案

#1


279  

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 $url dynamically with PHP, take a look at this question:

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

Get the full URL in PHP

在PHP中获取完整的URL

#2


74  

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

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

echo $_GET['email'];

will extract the emails from urls.

将从url中提取电子邮件。

#3


28  

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/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


8  

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

在URL中使用$_GET['email']作为参数。使用$_POST['email']将数据发送到脚本。或两者都使用_$REQUEST。另外,如前所述,您可以使用parse_url()函数,该函数返回URL的所有部分。使用一个叫做“查询”的部分——在那里你可以找到你的电子邮件参数。更多信息:http://php.net/manual/en/function.parse-url.php。

#5


5  

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

你可以用下面的代码来获得电子邮件地址?在URL中

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

#6


2  

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

我从@Ruel的回答中创建了函数。您可以使用:

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

由于@Ruel

#7


2  

You could get the parameters of the url like this:

你可以得到url的参数如下:

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:

如果url是这样的:

https://example.com/file.php

then the path to file is:

则文件路径为:

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

#8


0  

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

This is working Grate for me using php

这是使用php为我提供的服务

#9


-4  

you can use filter_input() see php manual

可以使用filter_input(),请参阅php手册

#1


279  

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 $url dynamically with PHP, take a look at this question:

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

Get the full URL in PHP

在PHP中获取完整的URL

#2


74  

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

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

echo $_GET['email'];

will extract the emails from urls.

将从url中提取电子邮件。

#3


28  

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/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


8  

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

在URL中使用$_GET['email']作为参数。使用$_POST['email']将数据发送到脚本。或两者都使用_$REQUEST。另外,如前所述,您可以使用parse_url()函数,该函数返回URL的所有部分。使用一个叫做“查询”的部分——在那里你可以找到你的电子邮件参数。更多信息:http://php.net/manual/en/function.parse-url.php。

#5


5  

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

你可以用下面的代码来获得电子邮件地址?在URL中

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

#6


2  

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

我从@Ruel的回答中创建了函数。您可以使用:

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

由于@Ruel

#7


2  

You could get the parameters of the url like this:

你可以得到url的参数如下:

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:

如果url是这样的:

https://example.com/file.php

then the path to file is:

则文件路径为:

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

#8


0  

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

This is working Grate for me using php

这是使用php为我提供的服务

#9


-4  

you can use filter_input() see php manual

可以使用filter_input(),请参阅php手册