- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
在一项学校作业中,我使用 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/
我有一个带有 的表格我想在服务器端捕获该文本区域中的任何换行符,并将它们替换为 . 这可能吗? 我尝试设置 white-space:pre在 textarea 上的 CSS,但仍然不够。 最佳答案
我正在通过 FileStream 更改文件(这是一个非常大的文件,我只需要更改标题而不重写整个文件。 该文件可以有 Unix 或 Windows 换行符,知道哪一个对我来说很重要,这样我可以在更新文件
如何以编程方式(不使用 vi)将 DOS/Windows 换行符转换为 Unix 换行符? dos2unix 和 unix2dos 命令在某些系统上不可用。 如何使用 sed、awk 和 tr 等命令
我编辑了一个 BibteX 文件,到目前为止,它显示了我需要的所有信息。 FUNCTION {electronic} { output.bibitem format.btitle "title"
您的软件是否处理来自其他系统的换行符? Linux/BSD linefeed ^J 10 x0A Windows/IBM return linefeed ^M^
我发现 Windows 命令行重定向会自动将 '\n' 替换为 '\r\n'。有什么方法可以避免这种情况?因为在 stdout 或 stderr 重定向之后,如果您将 '\r\n' 写入控制台,您将得
来自 this question ,这个…… lines = foo.value.split(/\r\n|\r|\n/); 是拆分字符串的一种方法,但如何用换行符将其连接回去? 另外,我想知道如果我说
我正在尝试获取新行,但如果我使用 \n 它不起作用。 任何通过向字符串添加一些东西来换行的方法,例如 \r\n (这也不起作用) gfx.DrawString("Project No \n" + te
我有一串数据,中间有换行符。例如: "Product Name \n Product Color \n Product Quantity \n Product Location \n Product
我正在尝试让 scmCommentPrefix 按照 http://maven.apache.org/plugins/maven-release-plugin/faq.html#scmCommentP
如何检查正则表达式 /^\n/在字符串中 blahblahblah 我似乎无法从 php 的 preg_match 获得返回值 1 . 编辑: 由于某种原因,我的坏处是 CR 本身就是我的换行符。
我很难在与文本字符串相同的行上输出变量。当我使用Write-Host而不是Write-Output时,它可以工作。我想使用Write-Output,因为这似乎是最佳做法(将内容保留在管道中),但是Wr
我正在Powershell中工作,以为here字符串中的特定单词着色。除包含回车/换行符的单词外,其他功能均有效。没有这些字符,如何计算单词的长度? 以下是我正在使用的功能和测试数据。我希望第二行上的
我有一个在Powershell中运行的脚本,并且我希望能够在脚本名称和脚本内容本身之间的结果文本文件输出中添加一行。 当前,从下面开始,行$str_msg = $file,[System.IO.Fil
有人知道smalltalk中字符串的换行符是什么吗? 我试图将字符串拆分为单独的行,但我无法弄清楚smalltalk 中的换行符是什么。 即。 string := 'smalltalk is
我有以下 printf 语句: printf ("%s (%s)\n",$row["word"], $row["definition"]); 我正在尝试解决换行符而不是: word defin
这个问题已经有答案了: how to use dotall flag for regex.exec() (4 个回答) 已关闭 7 年前。 字符串内容
我想用 CSS 换行。我正在使用内容。 td:before { content: "Test\A Test2"; } 它不工作。如何正确
考虑一个具有 UNIX 行结尾的 C++ 文件(即 '\x0a' 而不是 "\x0d\x0a")并包含以下原始字符串文字: const char foo[] = R"(hello^M )"; (其中
我有以下 printf 语句: printf ("%s (%s)\n",$row["word"], $row["definition"]); 我正在尝试解决换行符而不是: word defin
我是一名优秀的程序员,十分优秀!