用php替换文本文件中的valuie
我有一个具有以下设计的文本文件: 约翰·史密斯| 1|john@smith.com|无密码管理用php替换文本文件中的valuie,php,string,file,text,replace,Php,String,File,Text,Replace,我有一个具有以下设计的文本文件: 约翰·史密斯| 1|john@smith.com|无密码管理 汤姆·史密斯| 3|tom@smith.com|admin123|用户 等等。 每个字段都由“|”分隔。 我需要做的是替换此文本文件中的值,例如用户的密码或帐户类型 汤姆·史密斯|5|tom@smith.com|admin1234|管理 我成功地找到了行并使用explode()将其吐出,但是如何修改该值并将其写回文本文件 $name = $_POST['name']; $mydb = file('.
汤姆·史密斯| 3|tom@smith.com|admin123|用户
等等。 每个字段都由“|”分隔。 我需要做的是替换此文本文件中的值,例如用户的密码或帐户类型 汤姆·史密斯|5|tom@smith.com|admin1234|管理 我成功地找到了行并使用explode()将其吐出,但是如何修改该值并将其写回文本文件
$name = $_POST['name'];
$mydb = file('./DB/users.txt');
foreach($mydb as $line) {
if(stristr($line,$name)) $pieces = explode("|", $line);
$position = str_replace(array("\r\n","\r"),"",$pieces[1]);
$email = str_replace(array("\r\n","\r"),"",$pieces[2]);
$password = str_replace(array("\r\n","\r"),"",$pieces[3]);
$atype = str_replace(array("\r\n","\r"),"",$pieces[4]);
PHP提供了读取、写入和附加到文件的选项 您需要打开该文件,读取其中的所有内容,然后覆盖该文件。您将使用append获得重复的条目 下面是一个示例代码,来自:
对于您的代码:
<?php
$name = $_POST['name'];
$mydb = file('./DB/users.txt');
$output = array();
foreach($mydb as $line) {
if(stristr($line,$name)) {
$pieces = explode("|", $line);
$position = str_replace(array("\r\n","\r"),"",$pieces[1]);
$email = str_replace(array("\r\n","\r"),"",$pieces[2]);
$password = str_replace(array("\r\n","\r"),"",$pieces[3]);
$atype = str_replace(array("\r\n","\r"),"",$pieces[4]);
// Glue the variables back together and overwrite $line so that it gets up in the array
$line = $name & "|" $position & "|" $email & "|" $password & "|" $atype & "\r";
}
// put everything in the $output array so that it can be writen to file after this loop
array_push($output, $line);
}
fclose($mydb);
// Write your output to file
$mydb = fopen('./DB/users.txt', "w") or die("Unable to open file!");
foreach($output as $line) {
fwrite($mydb, $line);
}
fclose($mydb);
?>
有多种方法可以做到这一点。这里有一条路。我对代码进行了注释,以解释它是如何工作的
$name = $_POST['name'];
// use FILE_IGNORE_NEW_LINES so you won't have to deal with the line breaks
$mydb = file('./DB/users.txt', FILE_IGNORE_NEW_LINES);
// use a reference (&$line) so the code in your foreach loop modifies the $mydb array
foreach ($mydb as &$line) {
if (stristr($line,$name)) {
// assign the elements of the row to variables
list($name, $number, $email, $password, $type) = explode('|', $line);
// change whatever you need to change
$password = 'new password';
$type = 'new type';
// overwrite the line with the modified values
$line = implode('|', [$name, $number, $email, $password, $type]);
// optional: break out of the loop if you only want to do the replacement
// for the first item found that matches $_POST['name']
break;
}
};
// overwrite the file after the loop
file_put_contents('./DB/users.txt', implode("\n", $mydb));
你能把你现有的代码放在这里吗?你是想全部修改它们,还是修改一个特定的代码?还有,你考虑过使用数据库吗?这看起来更像是数据库的作业。$name=$\u POST['name']$mydb=file('./DB/users.txt');foreach($line形式的mydb){if(stristr($line,$name))$pieces=explode(“|“,$line);$position=str_replace(数组(“\r\n”,““\r”),”,“,”,$pieces[1]);$email=str replace(数组(“\r\n”,““,”\r”),“,”,“$pieces[3])$atype=str replace(数组(“\r\n”、“\r”)、“,$pieces[4]);}一个数据库没关系,但这更容易使应用程序在工作时可移植。
$name = $_POST['name'];
// use FILE_IGNORE_NEW_LINES so you won't have to deal with the line breaks
$mydb = file('./DB/users.txt', FILE_IGNORE_NEW_LINES);
// use a reference (&$line) so the code in your foreach loop modifies the $mydb array
foreach ($mydb as &$line) {
if (stristr($line,$name)) {
// assign the elements of the row to variables
list($name, $number, $email, $password, $type) = explode('|', $line);
// change whatever you need to change
$password = 'new password';
$type = 'new type';
// overwrite the line with the modified values
$line = implode('|', [$name, $number, $email, $password, $type]);
// optional: break out of the loop if you only want to do the replacement
// for the first item found that matches $_POST['name']
break;
}
};
// overwrite the file after the loop
file_put_contents('./DB/users.txt', implode("\n", $mydb));