所以我这里有一些代码可以从标准的Web表单中获取用户输入:
if (get_magic_quotes_gpc()) {
$searchsport = stripslashes($_POST['sport']);
$sportarray = array(
"Football" => "Fb01",
"Cricket" => "ck32",
"Tennis" => "Tn43",
);
if (isset($sportarray[$searchsport])) {
header("Location: " . $sportarray[$searchsport] . ".html");
die;
}
我如何修改这个(我认为这个词正在解析?)以使其 敏感?例如,我输入“fOoTbAlL”,PHP会直接指向Fb01.html。
请注意,代码只是一个示例。用户输入的字符串可以包含多个单词,例如“Crazy aWesOme HarpOOn-Fishing”,如果数组元素“Crazy Awesome Harpoon-Fishing”(请注意首都F
之前)它仍然有用破折号。
答案 0 :(得分:2)
我会使用字符串函数strtolower()
。
答案 1 :(得分:2)
最简单的方法是使用strtolower将所有内容设为小写以进行比较。
答案 2 :(得分:2)
您可以像这样修改代码:
// Searches for values in case-insensitive manner
function in_arrayi($needle, $haystack) {
return in_array(strtolower($needle), array_map('strtolower', $haystack));
}
$searchsport = $_POST['sport'];
$sportarray = array(
"Football" => "Fb01",
"Cricket" => "ck32",
"Tennis" => "Tn43",
);
if(in_arrayi($searchsport, $sportarray)){
header("Location: " . $sportarray[$searchsport] . ".html");
die;
}
答案 3 :(得分:1)
$searchsport = strtolower($_POST['sport']);
$sportarray = array(
"football" => "Fb01",
"cricket" => "ck32",
"tennis" => "Tn43",
);
if (isset($sportarray[$searchsport])){
header("Location: " . $sportarray[$searchsport] . ".html");
die;
}
通过这种方式,搜索字符串和数组键都是小写的,您可以进行不区分大小写的比较。
如果您想保留$sportarray
键的大小写,请执行以下操作:
$searchsport = ucfirst(strtolower($_POST['sport']));
$sportarray = array(
"Football" => "Fb01",
"Cricket" => "ck32",
"Tennis" => "Tn43",
);
if (isset($sportarray[$searchsport])){
header("Location: " . $sportarray[$searchsport] . ".html");
die;
}
答案 4 :(得分:0)
<?php
$searchsport = $_POST['sport'];
$sportarray = array(
"Football" => "Fb01",
"Cricket" => "ck32",
"Tennis" => "Tn43",
);
if(isset($sportarray[ucfirst(strtolower($searchsport]))])){
header("Location: ".$sportarray[$searchsport].".html");
die;
}
?>
注意ucfirst(strtolower($searchsport]))
?
LE:添加了ucfirst