我有一个名字.txt'文本文件的名称,如约翰,迈克,朱莉娅。现在我在附加模式中输入另一组名称到现有的名称.txt'档案像史蒂夫,本,迈克。 我怎样才能限制麦克'在php中重复输入?或者我如何提示错误消息,例如Mike,名字已经存在。请输入其他名称'这样可以避免重复。
代码是
<!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);
}
?>
答案 0 :(得分:5)
将文本文件条目与PHP_EOL
分开时:
(注意 - 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";
答案 1 :(得分:2)
可能最好的方法是
答案 2 :(得分:2)
<!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);
}
$currentNamesContainer = explode(PHP_EOL, $current_names);
/*** Get names from text box and Put to the 'names.txt' file in append mode ***/
if(isset($_POST['Save'])){
$newName = $_POST["textbox"]; // changed variable name to NewName
// If new name does not exist in current container, then just append it...otherwise bail!
if (!in_array($newName, $currentNamesContainer)) {
file_put_contents("names.txt", PHP_EOL.$newName, 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);
}
?>
算法: $ existingData = file_get_contents('names.txt');
// Now parse this string and store names in an array (list of names), that depends on your format of file...
$yourNewName = 'Mike';
// Now check is this exists in your current array.
if (in_array($yourNewName, $currentArray)) {
// exit with error
}
// Insert new name .
//This is pseudo code i have written. If you post your code, i can try more help.
答案 3 :(得分:2)
试试这个:
/* 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";
}
?>
答案 4 :(得分:2)
这么简单..
$data = file('names.txt', FILE_IGNORE_NEW_LINES);
$search = 'Jakarta';
echo (in_array($search, $data)) ? "Already exists" : "Available";