PHP:如何从文本文件中删除最后一个换行符?

标签 php arrays text explode

在一项学校作业中,我使用 PHP 为我所在城市的虚构太空博物馆构建了一个网站。它有一个数据包含和数据咨询系统,但我有一个咨询问题,我想知道如何解决:如何从文件中删除最后一个换行符?

数据包含

在网站的限制区域,我有一个包含 5 个字段(姓名、地址、电话号码、性别和参观过的展览)的 HTML5 表单,它通过 POST 方法将数据发送到 PHP 中的一个函数,该函数将数据写在使用 fwrite 给出 txt 文件命令:

fwrite ($pointer, "$name | $addres | $telephone | $sex | $exhibitions " .PHP_EOL);

如您所见,它将在表单中输入的数据加上一个换行符写入一个 txt 文件。指针是fopen使用的变量打开我需要工作的文件。输出示例:

Márcio Aguiar | Belmiro Braga Street | 1234-5678 | M | Planets of Solar System
Joana Tobias | Santos Dummont Avenue | 8765-4321 | F | Black Holes, Satellites

资料咨询

然后是咨询系统。它有一个循环,一直运行到文件结束。在这个循环中有一个名为 $buffer 的变量每次获取一行 txt 文件。然后是explode d 创建一个名为 $lines[$counter] 的数组.为了很好地打印它,我使用了 array_combine我将另一个数组 ( $keys ) 上的字段名称连接到 $lines[$counter] 中写入的值,并将其归因于 $combined[$counter] .然后循环结束,我使用 print_r里面<pre></pre>查看$combined中写入的数据,同时保留 HTML 会忽略的空格和换行符。这是代码:

$keys = array ("Name", "Address", "Telephone", "Sex", "Visited exhibition");
for ($counter=0;!feof($reader);$counter++){
    $buffer = fgets($reader);
    $lines[$counter] = explode(" | ", $buffer);
    $combined[$counter] = array_combine($keys, $lines[$counter]);
}
echo "<pre>";
print_r($combined);
echo "</pre>";

输出示例:

    Array
    (
    [0] => Array
        (
            [Name] => Márcio Aguiar
            [Address] => Belmiro Braga Street
            [Telephone] => 1234-5678
            [Sex] => M
            [Visited exhibitions] => Planets of Solar System 

        )

    [1] => Array
        (
            [Name] => Joana Tobias
            [Address] => Santos Dummont Avenue
            [Telephone] => 8765-4321
            [Sex] => F
            [Visited exhibitions] => Black Holes, Satellites 

        )

    [2] => 
    )

在这里你可以看到一个 2数组创建为空白。这是由最后一行引起的,它只包含上面表格插入的换行符。我需要删除最后一个换行符,只有那个换行符,但不知道如何。我想知道!当执行到达 array_combine 时,不知道会导致错误显示,因为需要两个数组具有相同数量的元素,并且 2是空白的。这里的错误:

Warning: array_combine(): Both parameters should have an equal number of elements in E:\Aluno\Documents\Wamp\www\trab_1\area_restrita\consulta.php on line 60

最佳答案

原答案:

要从任何文本中删除尾随换行符,您可以使用 trim() ,但是在您的情况下,您只需要在 append mode 中使用 fopen :

$handle = fopen("/path/to/file", "a");

然后摆脱 PHP_EOL:

fwrite ($pointer, "$name | $addres | $telephone | $sex | $exhibitions");

编辑:您说得对,附加不会附加到新行。我错了。所以你可以像我之前提到的那样使用 trim() 。我使用 file_get_contents()file_put_contents() 创建了一个快速示例,它似乎可以执行您想要的操作:

<?php

$file = 'test.txt';

// Set existing contents (for testing sake)
$orig_contents = "bob | 123 fake street | 1234567890 | yes, please | no\n";
$orig_contents .= "bob | 123 fake street | 1234567890 | yes, please | no\n";
$orig_contents .= "bob | 123 fake street | 1234567890 | yes, please | no\n";
$orig_contents .= "bob | 123 fake street | 1234567890 | yes, please | no\n";
file_put_contents($file, $orig_contents);

// Here is how you could add a single new line with append mode
// Notice the trailing \n
file_put_contents($file, "billy | 456 fake street | 2345678901 | no | yes\n", FILE_APPEND);

// Get contents from the file and remove any trailing line breaks
$contents = trim(file_get_contents($file));

$keys = array ("Name", "Address", "Telephone", "Sex", "Visited exhibition");

// Explode based on the new line character
$lines = explode("\n", $contents);

foreach ($lines as $line) {
    $values = explode(" | ", $line);
    $combined[] = array_combine($keys, $values);
}

print_r($combined);

这打印:

Array
(
    [0] => Array
        (
            [Name] => bob
            [Address] => 123 fake street
            [Telephone] => 1234567890
            [Sex] => yes, please
            [Visited exhibition] => no
        )

    [1] => Array
        (
            [Name] => bob
            [Address] => 123 fake street
            [Telephone] => 1234567890
            [Sex] => yes, please
            [Visited exhibition] => no
        )

    [2] => Array
        (
            [Name] => bob
            [Address] => 123 fake street
            [Telephone] => 1234567890
            [Sex] => yes, please
            [Visited exhibition] => no
        )

    [3] => Array
        (
            [Name] => bob
            [Address] => 123 fake street
            [Telephone] => 1234567890
            [Sex] => yes, please
            [Visited exhibition] => no
        )

    [4] => Array
        (
            [Name] => billy
            [Address] => 456 fake street
            [Telephone] => 2345678901
            [Sex] => no
            [Visited exhibition] => yes
        )

)

关于PHP:如何从文本文件中删除最后一个换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30041944/

相关文章:

string - Solr/Lucene : Possible to have Strings tokenized?

swift - 将 UILabel 的宽度精确调整为标签中文本的长度

php - 使用 PHP/Javascript 为移动设备重定向单个 PHP 页面的最佳方法

java - 查找数组中的重复项并仅打印一次

c++ - 无法在类中使用以数组作为参数的函数模板

javascript - 从对象数组创建一个新对象

java - 每次用户保存库时保存到新的 txt 文件

php - ajax调用后jquery不工作?

php - 在没有 formhelper 的情况下使用 CakePHP 检索 POST 数据

php - 根据页面url显示不同的侧边栏内容 - October CMS