根据 url 参数的值更改变量
Change a variable depending on value of url parameter
是否有 php 方法来根据 url 中某些参数的值更改 php 变量?
例如,我有这个特定的 url :
http://example.com/post-url-that-contains-value2/?custom_parameter=value1-value2-value3
我想做的是检查值 2(文本字符串)是否仅在 custom_parameter 中存在而不检查 post url(不幸的是包含与值 2)。当我在 custom_parameter 中检查并找到值 2 时,然后将 $myphp 变量更改为特定值。
我做的是这样做的:
$checkurl = $_SERVER['QUERY_STRING'];
if(preg_match('/^(?=.*custom_parameter)(?=.*value2).*$/m', $checkurl) === 1) {
$myphpvariable = 'Found!';
}
else {
$myphpvariable = 'NOT Found!';
}
不幸的是,此方法会检查整个 url,即使 URL 为 http://example.com/post-url-that-contains-value2/?custom_parameter=value3
,它也会将 $myphpvariable
更改为 'Found!'
例如....因为它在 post url.
中看到 value2
关于如何使其正常工作的任何想法?
可以分别查看uri和参数
//explode the url on the ? and get the first part, the uri
$uri = explode('?', $_SERVER['REQUEST_URI'])[0];
//get everything in custom_parameter
$customParameter = $_GET['custom_parameter'];
//check value2 is in not in the uri and is in the params
if(strpos($uri, 'value2') === false && strpos($customParameter, 'value2') !== false){
$myphpvariable = 'Found!';
}
else {
$myphpvariable = 'NOT Found!';
}
或者如果您只想检查 custom_parameter 并忽略 url
//get everything in custom_parameter
$customParameter = $_GET['custom_parameter'];
if(strpos($customParameter, 'value2') !== false){
$myphpvariable = 'Found!';
}
else {
$myphpvariable = 'NOT Found!';
}
我不会查看整个 url,而是使用 $_GET
数组,因为它是自己访问查询字符串参数的最简单方法。
strpos()
可能是使用 $_GET
数组搜索特定文本的最快和最简单的方法,但您也可以使用类似的方法,因为您的值都由相同的分隔符分隔.这样它将把 -
字符上的 custom_parameter
值字符串拆分成一个数组,然后在该数组中搜索 value2
。如果您想稍后搜索其他值,这可能会更有用。
$customParamater = $_GET["custom_parameter"];
$values = explode("-",$customParamater);
if (in_array("value2",$values)) {
$myphpvariable = 'Found!';
} else {
$myphpvariable = 'NOT Found!';
}
是否有 php 方法来根据 url 中某些参数的值更改 php 变量?
例如,我有这个特定的 url :
http://example.com/post-url-that-contains-value2/?custom_parameter=value1-value2-value3
我想做的是检查值 2(文本字符串)是否仅在 custom_parameter 中存在而不检查 post url(不幸的是包含与值 2)。当我在 custom_parameter 中检查并找到值 2 时,然后将 $myphp 变量更改为特定值。
我做的是这样做的:
$checkurl = $_SERVER['QUERY_STRING'];
if(preg_match('/^(?=.*custom_parameter)(?=.*value2).*$/m', $checkurl) === 1) {
$myphpvariable = 'Found!';
}
else {
$myphpvariable = 'NOT Found!';
}
不幸的是,此方法会检查整个 url,即使 URL 为 http://example.com/post-url-that-contains-value2/?custom_parameter=value3
,它也会将 $myphpvariable
更改为 'Found!'
例如....因为它在 post url.
关于如何使其正常工作的任何想法?
可以分别查看uri和参数
//explode the url on the ? and get the first part, the uri
$uri = explode('?', $_SERVER['REQUEST_URI'])[0];
//get everything in custom_parameter
$customParameter = $_GET['custom_parameter'];
//check value2 is in not in the uri and is in the params
if(strpos($uri, 'value2') === false && strpos($customParameter, 'value2') !== false){
$myphpvariable = 'Found!';
}
else {
$myphpvariable = 'NOT Found!';
}
或者如果您只想检查 custom_parameter 并忽略 url
//get everything in custom_parameter
$customParameter = $_GET['custom_parameter'];
if(strpos($customParameter, 'value2') !== false){
$myphpvariable = 'Found!';
}
else {
$myphpvariable = 'NOT Found!';
}
我不会查看整个 url,而是使用 $_GET
数组,因为它是自己访问查询字符串参数的最简单方法。
strpos()
可能是使用 $_GET
数组搜索特定文本的最快和最简单的方法,但您也可以使用类似的方法,因为您的值都由相同的分隔符分隔.这样它将把 -
字符上的 custom_parameter
值字符串拆分成一个数组,然后在该数组中搜索 value2
。如果您想稍后搜索其他值,这可能会更有用。
$customParamater = $_GET["custom_parameter"];
$values = explode("-",$customParamater);
if (in_array("value2",$values)) {
$myphpvariable = 'Found!';
} else {
$myphpvariable = 'NOT Found!';
}