PHP:分解文件并仅输出具有值的部分

发布于 2024-12-20 22:22:53 字数 1167 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

千紇 2024-12-27 22:22:53

您可以使用 fgetcsv()

注意:CSV 文件中的空行将作为包含单个空字段的数组返回,并且不会被视为错误。

<table>
<?php
$handle = fopen("test.csv", "r");
while (($parts = fgetcsv($handle, 1000, ",")) !== FALSE) {
    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>

You can use fgetcsv()

Note: A blank line in a CSV file will be returned as an array comprising a single null field, and will not be treated as an error.

<table>
<?php
$handle = fopen("test.csv", "r");
while (($parts = fgetcsv($handle, 1000, ",")) !== FALSE) {
    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>
默嘫て 2024-12-27 22:22:53

您可以将其替换为以下内容,而不是像您所知道的那样回显所有部分(10 行):

for($i = 1; $i <= 10; $i++) {
    echo '<td>' . (!empty($parts[$i]) ? $parts[$i] : '') . '</td>';
}

它的作用是:循环 10 次并始终回显 .在内部,它检查该迭代的 $parts[] 是否不为空,如果不是则回显它。否则它什么也没有回响。

你仍然希望所有的都在那里,否则你的桌子会被毁掉(对最终用户来说看起来很奇怪)。

Instead of echo'ing all the parts like you do know (the 10 lines), you could replace it by something like this:

for($i = 1; $i <= 10; $i++) {
    echo '<td>' . (!empty($parts[$i]) ? $parts[$i] : '') . '</td>';
}

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).

一个人的旅程 2024-12-27 22:22:53

未定义偏移错误是一个E_NOTICE,这意味着可以安全地忽略它。要防止显示通知,请使用:

error_reporting(E_ALL ^ E_NOTICE);

如果您不想这样做,可以通过多种方法解决该问题。一个技巧是将您的 explode 行更改为:

$parts = explode(",", $line_of_text.",,,,,,,,,,");

这样总会有至少 10 个部分。

The undefined offset error is an E_NOTICE, meaning it can safely be ignored. To prevent notices from showing up, use:

error_reporting(E_ALL ^ E_NOTICE);

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:

$parts = explode(",", $line_of_text.",,,,,,,,,,");

Such that there will always be at least 10 parts.

萌吟 2024-12-27 22:22:53
<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>";
for($i=1; $i<=10; $i++) {
    echo "<td>" . $parts[$i] ? $parts[$i] : '' . "</td>";
}
echo "</tr>";
}

fclose($file_handle);

?>
</table>
<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>";
for($i=1; $i<=10; $i++) {
    echo "<td>" . $parts[$i] ? $parts[$i] : '' . "</td>";
}
echo "</tr>";
}

fclose($file_handle);

?>
</table>
温馨耳语 2024-12-27 22:22:53
while (!feof($file_handle) )
{
  $line_of_text = fgets($file_handle);
  $parts = explode(',', $line_of_text);

  // line not empty?
  if (isset($parts[0]) && !empty($parts[0]))
  {
    // first line
    echo '<td>' . $parts[0] . '</td>';
    // other lines
    for($i = 1; $i <= 10; $i++) {
      // echo with check
      echo '<td>' . (isset($parts[$i]) ? $parts[$i] : '') . '</td>';
    }
  }
}
while (!feof($file_handle) )
{
  $line_of_text = fgets($file_handle);
  $parts = explode(',', $line_of_text);

  // line not empty?
  if (isset($parts[0]) && !empty($parts[0]))
  {
    // first line
    echo '<td>' . $parts[0] . '</td>';
    // other lines
    for($i = 1; $i <= 10; $i++) {
      // echo with check
      echo '<td>' . (isset($parts[$i]) ? $parts[$i] : '') . '</td>';
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文