PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了PHP:如何从文本文件中删除最后一个换行符?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

数据包含

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

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

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

资料咨询

然后是一个咨询系统.它具有一个循环,直到文件结束为止.在此循环中,有一个名为$buffer的变量,该变量每次都获取txt文件的一行.然后将其分解以创建一个名为$lines [$counter]的数组.为了更好地打印它,我使用了array_combine,其中将另一个数组($keys)上的字段名称连接到以$lines [$counter]编写的值,并将其附加到$combined [$counter].然后循环结束,我在< pre>< / pre>中使用了print_r.查看以$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>";

输出例:

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

解决方法:

原始答案:

要从任何文本中删除尾随换行符,可以使用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:如何从文本文件中删除最后一个换行符?全部内容,希望文章能够帮你解决PHP:如何从文本文件中删除最后一个换行符?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: