简体   繁体   中英

Php file text parsing

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM