如果是空输入文本

时间:2015-07-21 10:17:41

标签: php csv post

我想在输入空或输入错误时显示输出“找不到匹配的记录”。 请帮我填写???

<?

$file  = fopen('file.csv', 'r');
$output ='';
if (isset($_POST['search'])) {
    $words = $_POST['search'];
    $words = array(preg_replace("#[^0-9a-z]#","",$words));

    $words = array_map('preg_quote', $words);
    // The argument becomes '/wii|guitar/i', which means 'wii or guitar, case-insensitive'
    $regex = '/'.implode('|', $words).'/i';
    if (???)
    {
        $output = "No matching records found";
    }else{
        while (($line = fgetcsv($file)) !== FALSE) {  
            list($name, $age, $hobbies) = $line;

            if(preg_match($regex, $hobbies)) {
                $output = "$name, $age, $hobbies<br />\n";
            }
        }
    }
}
<form action="searchTest.php" method="post">
    <input type="text" name="search" placeholder="">
    <input type="submit" value="Search" />
    <br>
    <?php echo $output; ?>

CSV file:
name, age, hobbies
Tom, 8, "football, soccer, baseball, wii, star wars, books"
Bill, 9, "football, baseball, ice hockey, basketball"
Sue, 8, "baseball, soccer, volleyball, bicycles, skating"
Mike, 22, "basketball, music, guitar, cartoons, books"
Ella, 20, "soccer, basketball, softball, clothes, books"
Tim, 19, "football, baseball, basketball, wii, cartoons"
Steven, 18, "baseball, soccer, star wars, cartoons, books"

asdjkljdsfklsjklfjsdklfjsdklfsdjklfsjdklfjsdklfsd

2 个答案:

答案 0 :(得分:1)

您可以使用:

success

http://php.net/manual/en/function.empty.php

如果您在搜索框中未输入任何内容,那么正则表达式变量的最终值为'// i',因此您必须执行以下操作

if (empty($regex))
    {
        $output = "No matching records found";
    }

答案 1 :(得分:0)

if (empty($regex) || $regex=="")
{
    $output = "No matching records found";
}