在学校工作中,我使用PHP在我的城市中为虚构的太空博物馆建立了一个站点.它具有数据包含和数据咨询系统,但是我有一个咨询问题,我想知道该如何解决:如何从文件中删除最后一个换行符?
数据包含
在站点的受限区域中,我有一个HTML5表单,其中包含5个字段(名称,地址,电话号码,性别和参观的展览),该表单通过POST方法将数据发送到PHP中的函数,该函数将其写入给定的txt文件中通过使用fwrite命令:
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文件的一行.然后将其分解以创建一个名为$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 "
";
print_r($combined);
echo "
";
输出示例:
06002
在这里您可以看到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()创建了一个简单的示例,它似乎可以满足您的要求:
$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
)
)