1

我使用文本文件作为我网站的一些数据的存储/数据库。我通过在其指定的持有者或元素上显示每个内容成功地在我的网页中输出了它,但不知何故我想用另一种方式来定制它以满足我的需要。

我有这段代码用于显示文本文件中的内容并根据使用情况放置它们。

<?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>

编辑:

但现在我想使用换行符 (\n) 作为分隔符而不是“|” 有没有办法做到这一点?

所以文本文件内容应该是这样的

your-email@mail.com
Subscriber
hello

而不是这个

your-email@mail.com | Subscriber | hello

我对任何建议、建议和想法持开放态度,请指导我完成。谢谢!

4

1 回答 1

0

使用fget

$handle = @fopen(FILE)
$lines = array();
while (false != ($line = fgets($handle)) {
    $lines[] = $line;
}

这将读取整个文件并将每一行放入$lines. 如果您只有 3 条线路,请调用fgets3 次。

于 2013-07-27T17:29:44.917 回答