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

Php 如何从URL字符串获取参数?,php,url-parsing,Php,Url Parsing,我有一个HTML表单字段$\u 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@te

我有一个HTML表单字段
$\u 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 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 等等

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


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

可以使用
$\u GET
数组访问
之后的所有参数。所以

echo $_GET['email'];
将从URL中提取电子邮件。

您可以使用和

$parts = parse_url($url);
parse_str($parts['query'], $query);
echo $query['email'];
如果要使用PHP动态获取
$url
,请查看以下问题:


使用
$\u GET['email']
获取URL中的参数。 使用
$\u POST['email']
将数据发布到脚本。 或者同时使用
\u$REQUEST
。 另外,如前所述,您可以使用
parse_url()
函数返回url的所有部分。使用一个名为“查询”的部分-在那里您可以找到您的电子邮件参数。更多信息:

使用和方法
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 ) 

您可以使用下面的代码获取电子邮件地址后?在URL中

$uri=$\u服务器[“请求uri”];
$URARRAY=分解('/',$uri);
$page_url=$uriArray[1];
$page_url2=$uriArray[2];
echo$page_url 我创建了一个函数。
您可以使用以下选项:

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;
    }
}
例如:

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

感谢@Ruel

从URL字符串获取参数,我使用了以下函数

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');
var getUrlParameter=函数getUrlParameter(sParam){
var sPageURL=decodeURIComponent(window.location.search.substring(1)),
sURLVariables=sPageURL.split(“&”),
sParameterName,
我
对于(i=0;i

若有许多URL字符串,那个么您可以使用loop从所有这些URL字符串中获取参数“email”,并将它们存储在数组中。

如其他答案中所述,最好的解决方案是使用

您需要结合使用
parse_url()

parse_url()
parse url并返回其组件,您可以使用
query
键获取查询字符串。然后应该使用解析查询字符串并返回的
parse_str()
将值转换为变量

$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

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

您可以使用
preg\u match()
从URL获取查询字符串的特定值

preg_match("/&?email=([^&]+)/", $url, $matches);
echo $matches[1]; // xyz2@test.com
您还可以使用
preg\u replace()
一行中完成这项工作

$email=preg\u replace(“/^https?:\/\/.\?*”email=([^&]+).$/”、“$1”、$url);
// xyz2@test.com
在Laravel中,我正在使用:

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

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

解析字符串url并获取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
}
调用函数以获取结果

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

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

因此,对于问题,您可以使用此选项从URL get参数获取电子邮件值:

$email=filter\u input(输入\u获取,'email',过滤\u清理\u电子邮件)

对于其他类型的变量,您可能希望使用
过滤器\u清理\u字符串

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

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

不妨养成这样获取变量的习惯。

我有点困惑,请详细说明问题……你是说/要求URL被视为字符串吗?如果你想从字符串中“匹配”电子邮件部分,就像在你的示例中一样,请使用正则表达式。可以像
/(email=\w\@\w\.\w)/
或更高级的匹配技术一样简单。只是给你一个想法。请参阅
preg_match
函数:OP的可能重复表示他没有从
地址栏获取URL。这些URL只是代码中的字符串。OP请求从字符串中提取变量,而不是当前页面中的URL。parse_url()是正确的答案,假设字符串格式正确。这不是问题的答案,但这是我在问谷歌的问题时寻找的答案,这可能不是问题的答案,但却是获取URL参数的正确且最简单的方法——如果设置正确:
https://example.com?email=myemail@example.com
$email=$\u GET['email']$电子邮件==='myemail@example.com';正常工作,但我在以下日志中遇到错误:PHP注意:未定义索引:queryal因此,如果它是查询中的最后一个字符,则会有一个问号。我使用了:
if(strpos($urlParts['path'],'?'){$urlParts['path']=substr($urlParts['path'],'path']),0,strpos($urlParts['path'],'?);}
将其从末尾删除。我不是从浏览器地址栏中获取这些字符串,但您发布的itOP解决方案是在询问PHP,不,请你试着详细解释一下答案。很难理解你上面的答案是什么意思。哇哦,我没看到最后一行关于的问题
echo getParamFromUrl('https://google.co.in?name=james&surname=bond','surname'); // bond will be o/p here
$email = $_GET['email'];
$email = filter_var( $email, FILTER_SANITIZE_EMAIL );