区分大小写的HTML搜索栏

时间:2013-06-08 23:21:33

标签: php html

我有这个搜索栏,当用户输入一个单词时,会出现搜索的文件名。为了更清楚地解释这一点,我将添加我目前正在处理的代码。

HTML

<form method="post" action="submit.php">
            <input name="search" type="text" value="">             
                <button type="submit" name="Submit">
                Submit</button>
                   </form>

submit.php

if (isset($_POST['search'])){
              header( 'Location: http://headerLocation/'.$_POST['search'].'.xml' ) ;}
              else {
                  echo "Warning, your request could not be completed";
              }

?>

这完全有效,除了它区分大小写,如果有任何额外空间也会出错。 例如,如果我搜索“hello”,并且文件名为“Hello”或“hello”,则搜索将失效。 有谁知道如何使这个几乎无知的案件和空间?谢谢。

5 个答案:

答案 0 :(得分:5)

您可以修剪POST数据并仅为文件使用小写字母,并将“搜索”转换为小写。

这样的事情: submit.php

if (isset($_POST['search'])){
              header( 'Location: http://headerLocation/'.strtolower(trim($_POST['search'])).'.xml' ) ;}
              else {
                  echo "Warning, your request could not be completed";
              }

?>

答案 1 :(得分:3)

使用strtolower($_POST['search']) $_POST['search']会将文件名设为小写。

将其与this answer结合使用可以解决问题

答案 2 :(得分:2)

避免空格使用这个 更改$ _POST ['search']以在标题函数

中修剪($ _ POST ['search'])

答案 3 :(得分:1)

当您搜索时:

   trim(strtolower($searchTerm))

答案 4 :(得分:1)

您可以使用

$search = strtolower($_POST['search']);
$searchtrim = trim($search);

if (isset($searchtrim)){
              header( 'Location: http://headerLocation/'.$searchtrim.'.xml' ) ;}
              else {
                  echo "Warning, your request could not be completed";
              }

?>