drot98385 2015-05-01 14:28
浏览 35

Php文件文本解析

I'm having a problem with the following php code:

Code

<?php
    $file = fopen("res/changelog","r");
    while(! feof($file))
    {
        $line = fgets($file);
        if (strlen($line) > 0){
            if (substr($line, 0, 1) === "#"){
                echo "<h2>", substr($line, 1), "</h2><br />"
            }
            else {
                echo "<p>", $line, "</p><br />";
            }
        }
    }

    fclose($file);
?>

What I'm trying to achieve is to parse text from a file and transform it into HTML elements using php. For some reason, when I try to load the page, it's white.

Can anyone tell me the possible cause of the issue and how to fix it? Thank you in advance

  • 写回答

1条回答 默认 最新

  • dqkf49487 2015-05-01 14:43
    关注

    I've managed to do what I wanted by recoding it differently. Here's what I use now:

    Code

    <?php
        $file = fopen("res/changelog","r");
        echo "<p>";
        while(! feof($file))
        {
            $line = fgets($file);
            if (substr($line, 0, 1) === "#")
            {
                echo "</p><h2>", substr($line, 1), "</h2><p>";
            }
            else
            {
                echo $line, "<br />";
            }
        }
        echo "</p>";
        fclose($file);
    ?>
    

    I still don't know why the previous code didn't work, so if you know what the cause may be, I'll be more than happy to read your explanation.

    I'm sorry for posting the question before taking some time to fix it.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画