删除第二行:
$f1=fopen('fff.txt','r')
$tmp=tempnam()//建立临时文件
$f2=fopen($tmp,'w')
$line_no=1//行号
while(!feof($f1)){
$line=fgets($f1)
if ($line_no!=2) fputs($f2,$line)
$line_no++
}
fclose($f1)
fclose($f2)
rename($tmp,'fff.txt')
删除内容为“user2”的行:
$f1=fopen('fff.txt','r')
$tmp=tempnam()//建立临时文件
$f2=fopen($tmp,'w')
while(!feof($f1)){
$line=fgets($f1)
if ($line!='user2') fputs($f2,$line)
}
fclose($f1)
fclose($f2)
rename($tmp,'fff.txt')
补充:这点举一反三都做不到呀,如果是我的学生我不会来做这个补充的~
$f1=fopen('fff.txt','r')
$tmp=tempnam()//建立临时文件
$f2=fopen($tmp,'w')
while(!feof($f1)){
$line=fgets($f1)
list($u,$p)=explode(':',$line)
if ($u!='user2') fputs($f2,$line)
}
fclose($f1)
fclose($f2)
rename($tmp,'fff.txt')
一共要有三份代码
test1.php 输入的页面
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>
<title>Test delete</title>
<meta http-equiv="Content-Type" content="text/html charset=utf-8" />
</head>
<body>
<form name = 'fromTxt' action = 'test2.php' method ='post' class="smart-green">
<span>Delete:</span></br></br>
<input type="text" name="ip_del" maxlength="50" style="height:20pxwidth:200px" onchange ="Value()" >
<input type = 'submit' name = 'submit' class="button" value="Delete" />
</form>
</body>
</html>
test2.php 处理数据的后台
<?php$DELETE = $_POST['ip_del'] /*之前输入的值的name*/
$data = file("ttt.txt")/*txt文件名*/
$out = array()
foreach($data as $line) {
if(trim($line) != $DELETE) {
$out[] = $line
}
if(trim($line) == $DELETE){
echo "<script>alert('Existe')</script>"
}
}
If ($data == $out){
echo "<script>alert('Not Existe')</script>"
}
$fp = fopen("ttt.txt", "w+")
flock($fp, LOCK_EX)
foreach($out as $line) {
fwrite($fp, $line)
}
flock($fp, LOCK_UN)
fclose($fp)
echo "<script>location.href='test1.php'</script>"/*返回主页面*/
?>
ttt.txt 存储信息文本文档
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)