0

テキスト ドキュメントの値とフォームから投稿された値を比較するコードを作成しようとしています。

これまでのところ、私はこれを手に入れましたが、私は何か間違ったことをしていると確信しています.

よろしくお願いします。

<form method="POST" action="file.php"> 
<p>
<br /><input type="text" name="name" ><br />
</p>
<p><input type="submit" value="Check" /></p>
</form>

<?php
if (isset($_POST['name'])) {
    $name = $_POST['name'];

    /*The text document contains these written names: 
    Michael 
    Terry 
    John 
    Phillip*/ 

    $lines = file('names.txt'); 

    $names_array = ($lines);   

        if (in_array($name, $names_array)) {
            echo "exists";
        } else {
            echo 'none';
        }
}
?>

更新: 修正され、正常に動作するようになりました!

4

3 に答える 3

-1



/*The text document contains these written names: 
Michael 
Terry 
John 
Phillip*/ 

$lines = file('data.txt'); //Lets say we got an array with these values   //$lines =array('Michael','John','Terry','Phillip');    $i=0;
foreach($lines as $line)   {
$lines[$i] =trim($line);
$i++;   }    

    if (in_array($name, $lines)) {
        echo "exists";
    } else {
        echo 'none';
    } } ?

引用符

data.txt

マイケル・テリー・ジョン・フィリップ

data.txt にはスペースが含まれているため、trim() を使用して削除します。

于 2013-05-16T07:04:56.727 に答える