如何在PHP中将重复数据输入限制为文本文件中的现有数据?
我有一个名为“names.txt”的文本文件,名字像约翰、迈克、朱莉娅。现在,我以附加模式在现有的“names.txt”文件中输入另一组名称,如Steve、Ben、Mike。 如何限制php中的“Mike”重复项?或者,我如何提示一条错误消息,比如“Mike,name已经存在”。请输入另一个名称“以便避免重复” 代码是如何在PHP中将重复数据输入限制为文本文件中的现有数据?,php,html,Php,Html,我有一个名为“names.txt”的文本文件,名字像约翰、迈克、朱莉娅。现在,我以附加模式在现有的“names.txt”文件中输入另一组名称,如Steve、Ben、Mike。 如何限制php中的“Mike”重复项?或者,我如何提示一条错误消息,比如“Mike,name已经存在”。请输入另一个名称“以便避免重复” 代码是 <!DOCTYPE html> <html> <head> <title>Form Page</title>
<!DOCTYPE html>
<html>
<head>
<title>Form Page</title>
</head>
<body>
<form action="Files.php" method="POST">
<textarea rows="15" cols="30" value="textbox" name="textbox"></textarea></br>
<input type="submit" value="Save" name="Save">
<input type="submit" value="Fetch" name="Fetch">
</form>
</body>
</html>
<?php
/*** Get names from 'names.txt' file and prints the names stored in it ***/
if(isset($_POST['Fetch'])){
$file_names = "names.txt";
$current_names = file_get_contents($file_names);
echo nl2br($current_names);
}
/*** Get names from text box and Put to the 'names.txt' file in append mode ***/
if(isset($_POST['Save'])){
$current_names = $_POST["textbox"];
file_put_contents("names.txt", PHP_EOL.$current_names, FILE_APPEND);
/*** Get names form 'names.txt' file and prints the names stored in it ***/
$file_names = "names.txt";
$current_names = file_get_contents($file_names);
echo nl2br($current_names);
}
?>
表格页
最好的方法可能是
使用
PHP\u EOL
分隔文本文件条目时:
(请注意,PHP\u EOL
的输出取决于PHP运行的操作系统!)
试试这个:
/* names.txt
mike
peter
louis
hamilton
*/
<!DOCTYPE html>
<html>
<head>
<title>Form Page</title>
</head>
<body>
<form action="Files.php" method="POST">
<textarea rows="15" cols="30" value="textbox" name="textbox"></textarea></br>
<input type="submit" value="Save" name="Save">
<input type="submit" value="Fetch" name="Fetch">
</form>
</body>
</html>
<?php
/*** Get names from 'names.txt' file and prints the names stored in it ***/
if(isset($_POST['Fetch'])){
$file_names = "names.txt";
$current_names = file_get_contents($file_names);
echo nl2br($current_names);
}
/*** Get names from text box and Put to the 'names.txt' file in append mode ***/
if(isset($_POST['Save']) && !empty($_POST['textbox'])){
$file_names = "names.txt";
$current_names_onfile = file_get_contents($file_names);
$current_names = $_POST["textbox"];
$arr = explode("\r", $current_names);
foreach($arr as $name)
{
#remove line breaks
$name = preg_replace('/[\r\n]/m', '', $name);
#if the name doesn't exist on file stores it.
if (!preg_match("/^\b$name\b\$/m", $current_names_onfile)) {
file_put_contents("names.txt", PHP_EOL.$name, FILE_APPEND);
}else{
echo "$name already exists<br />";
}
}
/*** Get names form 'names.txt' file and prints the names stored in it ***/
$file_names = "names.txt";
$current_names = file_get_contents($file_names);
echo nl2br($current_names);
}else if(isset($_POST['Save'])){
echo "please type something";
}
?>
/*names.txt
迈克
彼得
路易斯
汉密尔顿
*/
表格页
这么简单
$data = file('names.txt', FILE_IGNORE_NEW_LINES);
$search = 'Jakarta';
echo (in_array($search, $data)) ? "Already exists" : "Available";
发布你的代码以提供帮助。嗨,卢卡斯·亨里克,我已经发布了代码。请检查。@Latchu我发布了一个新的解决方案,请试用。只需将explode()函数中的分隔符替换为您使用的分隔符即可。似乎您正在使用“PHP\u EOL”->explode(PHP\u EOL,$file\u content);现在更改了我的答案以适应您更新的问题。请注意——“PHP_EOL”取决于PHP代码运行的操作系统。
//new name to insert if not in file:
$new_name = "Martin";
$file_content = file_get_contents('names.txt');
//names into array elements
$content_array = explode(PHP_EOL, $file_content);
//trim spaces and stuff from the data in your array:
foreach($content_array AS $key => $value)
{
$content_array[$key] = trim($value);
}
if(in_array($new_name, $content_array)) echo "name found - don't save it again";
/* names.txt
mike
peter
louis
hamilton
*/
<!DOCTYPE html>
<html>
<head>
<title>Form Page</title>
</head>
<body>
<form action="Files.php" method="POST">
<textarea rows="15" cols="30" value="textbox" name="textbox"></textarea></br>
<input type="submit" value="Save" name="Save">
<input type="submit" value="Fetch" name="Fetch">
</form>
</body>
</html>
<?php
/*** Get names from 'names.txt' file and prints the names stored in it ***/
if(isset($_POST['Fetch'])){
$file_names = "names.txt";
$current_names = file_get_contents($file_names);
echo nl2br($current_names);
}
/*** Get names from text box and Put to the 'names.txt' file in append mode ***/
if(isset($_POST['Save']) && !empty($_POST['textbox'])){
$file_names = "names.txt";
$current_names_onfile = file_get_contents($file_names);
$current_names = $_POST["textbox"];
$arr = explode("\r", $current_names);
foreach($arr as $name)
{
#remove line breaks
$name = preg_replace('/[\r\n]/m', '', $name);
#if the name doesn't exist on file stores it.
if (!preg_match("/^\b$name\b\$/m", $current_names_onfile)) {
file_put_contents("names.txt", PHP_EOL.$name, FILE_APPEND);
}else{
echo "$name already exists<br />";
}
}
/*** Get names form 'names.txt' file and prints the names stored in it ***/
$file_names = "names.txt";
$current_names = file_get_contents($file_names);
echo nl2br($current_names);
}else if(isset($_POST['Save'])){
echo "please type something";
}
?>
$data = file('names.txt', FILE_IGNORE_NEW_LINES);
$search = 'Jakarta';
echo (in_array($search, $data)) ? "Already exists" : "Available";