如何更新和删除文本文件上的指定字符串
问题描述:
I was using a text file as a storage/database for some few datas for my website. I successfully outputed it in my web page by displaying each contents on its specified holder or element but somehow I want to customize it using another way to cater my needs.
I have this code for displaying the contents from a text file and place them according to there use.
<?php
$handle = @fopen("mydb.txt", "r");
while (!feof($handle)) // Loop til end of file.
{
$buffer = fgets($handle, 4096);
// Read a line.
list($a,$b,$c)=explode("|",$buffer);
//Separate string by the means of |
}
$aa = $a;
$bb = $b;
$cc = $c;
?>
<div id="tabs-1">
<form id="dbform" action="processor.php" method="GET" >
<input type="hidden" name="login" value="emailsave" />
<div id="error"></div>
<table cellpadding="3" cellspacing="3" align="center" valign="top">
<tr>
<td valign="top">
<label>Senders email:</label>
</td>
<td>
<input class="dainput" type="text" name="mailsenders" value="<?php echo $aa;?>" />
</td>
</tr>
<tr>
<td valign="top">
<label>Subject:</label>
</td>
<td>
<input class="dainput" type="text" name="mailsubject" value="<?php echo $bb;?>" />
</td>
</tr>
<tr>
<td valign="top">
<label>Message:</label>
</td>
<td>
<textarea style="height: 150px;" class="dainput" type="text" name="mailmsg" ><?php echo $cc;?></textarea>
</td>
</tr>
</table>
<button class="dabutton">Update</button>
</form>
</div>
edit:
but now I want to use new line ( ) as a separator instead of "|" is there anyway to achieve that?
so the text file content should be like this
your-email@mail.com
Subscriber
hello
instead of this
your-email@mail.com | Subscriber | hello
Im open in any suggestions, recommendations and ideas, please guide me through. Thanks!
答
Use fgets
$handle = @fopen(FILE)
$lines = array();
while (false != ($line = fgets($handle)) {
$lines[] = $line;
}
That will read the entire file and put each line into $lines
.
If you have just 3 lines, call fgets
3 times.