我想输出一条消息,
每当url包含以p2
开头的任何参数时,例如在以下所有实例中:
example.com/?p2=hello
example.com/?p2foo=hello
example.com/?p2
example.com/?p2=
我试过了:
if (!empty($GET['p2'])) {
echo "a parameter that starts with p2 , is showing in your url address";
} else {
echo "not showing";
}
答案 0 :(得分:3)
这应该涵盖你的所有案件
$filtered = array_filter(array_keys($_GET), function($k) {
return strpos($k, 'p2') === 0;
});
if ( !empty($filtered) ) {
echo 'a paramater that starts with p2 , is showing in your url address';
}
else {
echo 'not showing';
}
答案 1 :(得分:1)
只需迭代$_GET
数组,并在匹配时根据需要为p2
添加键的条件。
foreach($_GET as $key=>$value){
if (substr($key, 0, 2) === "p2"){
// do your thing
print $value;
}
}
substr($key,0,2)
获取字符串
答案 2 :(得分:0)
试
if (isset($GET['p2'])) {
echo "a paramater that starts with p2 , is showing in your url address";
} else {
echo "not showing";
}
答案 3 :(得分:0)
最快的方法是
[]