【问题标题】:Parse Section names from ini in PHP从 PHP 中的 ini 解析节名称
【发布时间】:2014-08-12 11:39:03
【问题描述】:

我整晚都在弄乱这段代码,但一直没能完全正确,甚至不确定要搜索什么。

这就是我正在做的事情。我正在使用 PHP 将 .ini 文件解析为 HTML 表。

这是我在 html 文件中使用的内容:

<?php
  $datas  = parse_ini_string(file_get_contents( 'https://dl.dropboxusercontent.com/u/12345/test.ini' ), true);
?>
<table border="1" cellspacing="0" cellpadding="5">
  <tbody>
    <?php
      foreach( $datas as $data ) {
    ?>
    <tr>
      <td rowspan="2"><?php echo htmlspecialchars( $data["name"] ); ?></td>
      <td>Name</td>
      <td>Points</td>
    </tr>
    <tr>
      <td><?php echo htmlspecialchars( $data["Name"] ); ?></td>
      <td><?php echo htmlspecialchars( $data["Points"] ); ?></td>
      </td>
    </tr>
    <?php
      }
    ?>
  </tbody>
</table>

我想在桌子上显示的是下面标记为“第 n 部分”的内容:

[Section 1]
Points=3

[Section 2]
Points=173

[Section 3]
Points=173

我不清楚如何在表格中显示“Section n”,我希望它采用以下格式:

Section number  |  Points
Section 1       |  3
Section 2       |  173
Section 3       |  173

非常感谢任何帮助或指点!非常感谢!!

【问题讨论】:

    标签: php html parsing ini


    【解决方案1】:

    这是有效的代码,您可以使用密钥获取部分名称。

    <?php
      $datas  = parse_ini_string(file_get_contents( 'http://hastebin.com/raw/ikovafilib' ), true);
    ?>
    <table border="1" cellspacing="0" cellpadding="5">
      <tbody>
        <?php
          foreach( $datas as $section => $data ) {
        ?>
        <tr>
          <td rowspan="2"><?php echo htmlspecialchars( $data["name"] ); ?></td>
          <td>Name</td>
          <td>Points</td>
        </tr>
        <tr>
          <td><?php echo htmlspecialchars( $section ); ?></td>
          <td><?php echo htmlspecialchars( $data["Points"] ); ?></td>
          </td>
        </tr>
        <?php
          }
        ?>
      </tbody>
    </table>
    

    编辑:将来,当您处理不知道其输出的数组时,您可以使用 print_r() 函数来调试它的输出。

    【讨论】:

    • 纯粹的奇妙魔法!非常感谢您的帮助,效果很好!
    【解决方案2】:

    Ini parse_ini_file 部分名称被添加为键。

    尝试使用

    <?php
      foreach( $datas as $key => $data ) {
    ?>
    <tr>
      <td rowspan="2"><?php echo htmlspecialchars( $key ); ?></td>
      <td>Points</td>
    </tr>
    <tr>
      <td><?php echo htmlspecialchars( $key ); ?></td>
      <td><?php echo htmlspecialchars( $data["Points"] ); ?></td>
      </td>
    </tr>
    <?php
      }
    ?>
    

    【讨论】:

    • 这成功了!作为 $key => $data..man 我不会有世俗的想法!非常感谢!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    • 2013-02-17
    • 2020-06-16
    • 2013-05-23
    • 2013-04-11
    • 2019-04-16
    相关资源
    最近更新 更多