php 检查 URL 中是否存在参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5360014/
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 21:09:12  来源:igfitidea点击:

Check if parameters exist in an URL

phpurlparameters

提问by laukok

How can I check if an URL has parameters in it?

如何检查 URL 中是否包含参数?

for instance, if the string is like this,

例如,如果字符串是这样的,

form_page_add.php?parent_id=1

return true

But if it is like this,

但如果是这样的话

form_page_add.php?

return nothing

Thanks.

谢谢。

EDIT:

编辑:

Sorry for not being clear, the URL is submitted from a from as a string. and I will store that string in a variable,

抱歉不清楚,URL 是从 from 作为字符串提交的。我会将那个字符串存储在一个变量中,

if(isset($_POST['cfg_path'])) $cfg_path = trim($_POST['cfg_path']);

so I need to check this variable $cfg_path whetheris has parameters in it.

所以我需要检查这个变量$cfg_path whether是否有参数。

回答by barfoon

Could also look for a specific key with array_key_exists(), e.g.

也可以使用 array_key_exists() 查找特定键,例如

if(array_key_exists('some-key', $_GET))

http://php.net/manual/en/function.array-key-exists.php

http://php.net/manual/en/function.array-key-exists.php

回答by Jon

You can use this simple function:

您可以使用这个简单的函数:

function url_get_param($url, $name) {
    parse_str(parse_url($url, PHP_URL_QUERY), $vars);
    return isset($vars[$name]) ? $vars[$name] : null;
}

See it in action here.

在这里查看它的实际效果

It will return the value of the parameter if it exists in the url, or nullif it does not appear at all. You can differentiate between a parameter having no value and not appearing at all by the identical operator (triple equals, ===).

如果参数存在于 url 中,或者null根本没有出现,它将返回参数的值。您可以通过相同的运算符(三等号,===)区分没有值的参数和根本不出现的参数。

This will work with any URL you pass it, not just $_SERVER['REQUEST_URI'].

这将适用于您传递的任何 URL,而不仅仅是$_SERVER['REQUEST_URI'].

Update:

更新:

If you just want to know if there is any parameter at allin the URL then you can use some variant of the above (see Phil's suggestion in the comments).

如果您只想知道URL 中是否有任何参数,那么您可以使用上述的一些变体(请参阅 Phil 在评论中的建议)。

Or, you can use the surprisingly simple test

或者,您可以使用令人惊讶的简单测试

if (strpos($url, '=')) {
    // has at least one param
}
if (strpos($url, '=')) {
    // has at least one param
}

We don't even need to bother to check for falsehere, as if an equals sign exists it won't be the first character.

我们甚至不需要在false这里检查,就好像等号存在一样,它不会是第一个字符。

Update #2:While the method using strposwill work for mostURLs, it's not bulletproof and so should not be used if you don't know what kind of URL you are dealing with. As Steve Onzra correctly points out in the comments, URLs like

更新 #2:虽然使用的方法strpos适用于大多数URL,但它不是防弹的,因此如果您不知道要处理的 URL 类型,则不应使用。正如 Steve Onzra 在评论中正确指出的那样,URL 像

http://example.com/2012/11/report/cGFyYW1fd2l0aF9lcXVhbA==

are valid and yet do not contain any parameter.

是有效的,但不包含任何参数。

回答by arijeet

Another way to check would be to use parse_url()method. Check docs here. This function will return an associative array with an element 'query' which will contain the GET parameters.

另一种检查parse_url()方法是使用方法。在此处查看文档。此函数将返回一个关联数组,其中包含一个包含 GET 参数的元素“查询”。

Use the empty()function to check and see whether this field is empty or not. If empty, then no parameters have been passed.

使用该empty()函数检查并查看该字段是否为空。如果为空,则没有传递任何参数。

Code Sample -

代码示例 -

<?php

    $url = "http://www.sub.domain.com/index.php?key=value&key2=value2";
    print_r(parse_url($url));
?>

Output

输出

Array
(
    [scheme] => http
    [host] => www.sub.domain.com
    [path] => /index.php
    [query] => key=value&key2=value2
)

回答by dmcnelis

If you are looking for whether a URL stored as a string (instead of the URL that is being called to invoke the PHP script), you can use strpos()

如果您正在寻找是否将 URL 存储为字符串(而不是被调用以调用 PHP 脚本的 URL),您可以使用 strpos()

So you would be able to search the string for an occurence of ?, and then deal with it appropriately. For example:

因此,您将能够搜索字符串中是否出现 ?,然后适当地处理它。例如:

$pos = strpos($myString, "?");
if($pos && $pos<strlen($myString){
    //deal with URLs with parameters
}