PHP - 编辑/删除文件中的特定行 [英] PHP - Edit/Delete particular line in a file
问题描述
IDAccess; Expire>> 26-08-2013<<
IDAccess; Expire>> 27-08-2013<<
IDAccess;过期>> 28-08-2013<<
我不想检查Expire日期是否大于当前日期时间,如果是这样,我想在该行的开始处添加分号或者简单地删除该行。
到目前为止,我写的代码如下: 它检索'过期日期'从文件中的每一行。它还检查是否大于当前日期,但在这一点上我不知道如何编辑(通过在开始处添加分号)或删除满足条件的行。 试试这个: 如果您要编辑该行,请使用 如果你想删除那一行,什么都不要做。只要忽略。 如果您想切换到下一行,请将当前行按压到新数组中。 在最后保存 I have a file I wan't to check if the Expire date is greater than current datetime, and if so I want to add a semicolon at the begin of that line or simply delete that line. The code i wrote so far for that is following: It retrieves the 'expire date' from every single line from file. It also checks if it's greater than current date but at this point i don't know how to edit (by adding semicolon at the begin) or delete the line which satisfy the condition. Any suggestions? Try like this one: If you want to edit that line, use If you want to delete that line, do nothing. Just ignore. If you want switching to the next line, push currently line to new array. At final save 这篇关于PHP - 编辑/删除文件中的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
pre $ $ files = file('users.txt');
foreach($ files as $ line){
$ pattern ='/>>(.*)<</';
preg_match($ pattern,$ line,$ matches);
$ expiredate = strtotime($ matches [1]);
$ currdate = strtotime(date('d-m-Y'));
if($ currdate> $ expiredate){
echo'访问过期...编辑/删除行< br />';
} else {
echo'什么也不做,它确定 - >切换到下一行...< br />';
$ b code
$ b
$ files = file('users.txt');
$ new_file = array();
foreach($ files as $ line){
$ pattern ='/>>(.*)<</';
preg_match($ pattern,$ line,$ matches);
$ expiredate = strtotime($ matches [1]);
$ currdate = strtotime(date('d-m-Y'));
if($ currdate> $ expiredate){
//对于编辑
$ line = preg_replace('/ condition /','replace',$ line); //替换
$ new_file [] = $ line; //按下编辑过的行
//如果你删除这行,不要push数组并且什么也不做
} else {
$ new_file [] = $ line; // push line new array
}
}
file_put_contents('users.txt',$ new_file);
preg_match
,然后将编辑好的行按到新的数组中。
新数组
到文件。users.txt
which contains:"ID" "Access" ;Expire>>26-08-2013<<
"ID" "Access" ;Expire>>27-08-2013<<
"ID" "Access" ;Expire>>28-08-2013<<
$files = file('users.txt');
foreach ($files as $line) {
$pattern = '/>>(.*)<</';
preg_match($pattern, $line, $matches);
$expiredate = strtotime($matches[1]);
$currdate = strtotime(date('d-m-Y'));
if ($currdate > $expiredate) {
echo 'access expired... edit/delete the line<br/>';
} else {
echo 'do nothing, its ok -> switching to the next line...<br/>';
}
}
$files = file('users.txt');
$new_file = array();
foreach ($files as $line) {
$pattern = '/>>(.*)<</';
preg_match($pattern, $line, $matches);
$expiredate = strtotime($matches[1]);
$currdate = strtotime(date('d-m-Y'));
if ($currdate > $expiredate) {
// For edit
$line = preg_replace('/condition/', 'replace', $line); // Edit line with replace
$new_file[] = $line; // Push edited line
//If you delete the line, do not push array and do nothing
} else {
$new_file[] = $line; // push line new array
}
}
file_put_contents('users.txt', $new_file);
preg_match
and push edited line to new array.new array
to file.