PHP:分解文件并仅输出具有值的部分
Php菜鸟。
我有这个文本文件(names.txt),其中包含逗号分隔的名称和代码。看起来与此类似:
John doe, 123456, 876543
Mary Ann, 456878
Ben Anderson, 987554, 097532, 873445
如您所见,每个名称可以有不同数量的代码,范围从 1 到 10。 我想要做的是将这些信息输出为表格
我尝试过:
<table>
<?php
$file_handle = fopen("names.txt", "rb");
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode(',', $line_of_text);
echo "<tr>";
echo "<td><strong>" . $parts[0] ."</strong></td>";
echo "<td>" . $parts[1] . "</td>";
echo "<td>" . $parts[2] . "</td>";
echo "<td>" . $parts[3] . "</td>";
echo "<td>" . $parts[4] . "</td>";
echo "<td>" . $parts[5] . "</td>";
echo "<td>" . $parts[6] . "</td>";
echo "<td>" . $parts[7] . "</td>";
echo "<td>" . $parts[8] . "</td>";
echo "<td>" . $parts[9] . "</td>";
echo "<td>" . $parts[10] . "</td>";
echo "</tr>";
}
fclose($file_handle);
?>
</table>
这不起作用,因为有时 $parts[] 将为空,并且没有任何内容可输出,并且我收到错误。未定义的偏移量。 我将如何做到这一点并且仅在 $parts[] 具有值时“回显”它?
Php noob.
I have this text file (names.txt) with comma separated names and codes. Looks similar to this:
John doe, 123456, 876543
Mary Ann, 456878
Ben Anderson, 987554, 097532, 873445
As you can see each name can have a different amount of codes ranging from one up to 10.
What I want to do is to output this information as a table
I tried this:
<table>
<?php
$file_handle = fopen("names.txt", "rb");
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode(',', $line_of_text);
echo "<tr>";
echo "<td><strong>" . $parts[0] ."</strong></td>";
echo "<td>" . $parts[1] . "</td>";
echo "<td>" . $parts[2] . "</td>";
echo "<td>" . $parts[3] . "</td>";
echo "<td>" . $parts[4] . "</td>";
echo "<td>" . $parts[5] . "</td>";
echo "<td>" . $parts[6] . "</td>";
echo "<td>" . $parts[7] . "</td>";
echo "<td>" . $parts[8] . "</td>";
echo "<td>" . $parts[9] . "</td>";
echo "<td>" . $parts[10] . "</td>";
echo "</tr>";
}
fclose($file_handle);
?>
</table>
This does not work because sometimes $parts[] will be empty and there is nothing to output and I get an error. Undefined offset.
How would I go about doing this and only "echo" the $parts[] when it has a value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 fgetcsv()
You can use fgetcsv()
您可以将其替换为以下内容,而不是像您所知道的那样回显所有部分(10 行):
它的作用是:循环 10 次并始终回显 .在内部,它检查该迭代的 $parts[] 是否不为空,如果不是则回显它。否则它什么也没有回响。
你仍然希望所有的都在那里,否则你的桌子会被毁掉(对最终用户来说看起来很奇怪)。
Instead of echo'ing all the parts like you do know (the 10 lines), you could replace it by something like this:
What does it do: loop 10 times and echo always an . Inside the it checks if $parts[] for that iteration is not empty, and if not echos it. Else it echos just nothing.
You still want all 's to be there, else your table would get ruined (would look strange to the end user).
未定义偏移
错误是一个E_NOTICE
,这意味着可以安全地忽略它。要防止显示通知,请使用:如果您不想这样做,可以通过多种方法解决该问题。一个技巧是将您的
explode
行更改为:这样总会有至少 10 个部分。
The
undefined offset
error is anE_NOTICE
, meaning it can safely be ignored. To prevent notices from showing up, use:If you don't want to do this, there are a few ways you can fix the problem. One trick would be to change your
explode
line to:Such that there will always be at least 10 parts.