我希望以这样一种方式编写代码,即它将编写并保存.TXT文件中的任何输入。然后,应该有一些函数可以搜索整个文件(txt),如果一个文本或单词以前已经写过或保存过,那么它应该会带来错误。
如果有人写了“爱”,而另一个人想再次输入“爱”,它应该会带来一条错误信息。
这是我的密码
<?php
$name=$_POST['name'];
$myfile=
fopen("man.txt" , "a+") or
die("Unable to open file!");
fwrite($myfile, "Name:".$name. "\r\n")
?> 发布于 2017-04-20 08:48:55
在这里,我使用in_array函数检查我们填充的数组($users)中提交的名称。
如果找到名称,则回显“名称存在”。如果找不到,它会回显“否”并添加名称。
<?php
$name = $_POST['name'];
if (empty($name)) { echo "Please enter your name"; } else $name=$name;
$myfile= fopen("mam.txt" , "a+") or die("Unable to open file!");
$path="mam.txt";
if (file_exists($path)) {
$contents = file_get_contents($path);
$contents = explode("\r\n", $contents);
$users = array();
foreach ($contents as $value) {
$users[] = $value;
}
// Search the file for the submitted name
if (in_array($name, $users)) {
// The name already exists
echo "Name exist";
} else {
// The name doesnt exist
echo "NO";
// Add the new name
fwrite($myfile, $name. "\r\n");
}
}
?>https://stackoverflow.com/questions/43496908
复制相似问题