doubi9999 2013-07-27 15:58
浏览 36
已采纳

如何更新和删除文本文件上的指定字符串

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!

  • 写回答

1条回答 默认 最新

  • dpn68721 2013-07-27 17:29
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题