php - file() return array not acting right

Viewed 46

I have some php code which I believe should read the lines of a text file into an array, and then display a JavaScript alert box sequentially showing each line of the file.

It works fine if I use line 11 (i.e. using a basic array), but does not work if I read lines from a file (line 10).

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <div id="main">
            <?php
                //$text = file("C:\\Apache24\\htdocs\\release_notes_2.txt");      // line 10
                $text = array('line 1', 'line 2', 'line 3', 'line 4', 'line 5');  // line 11
                $it = 0;

                foreach ($text as $line) {
                    echo "<script type='text/javascript'>alert('".$text[$it]."');</script>";
                    $it++;
                }
            ?>
        </div>
    </body>
</html>

When I use line 10 instead of line 11, I only get one alert, and the alert message corresponds to the very last line of the file.

Why is this happening?

Note: I am using php version 7.0.14.

1 Answers

instead of echoing all the <script> tag, try to use one only, like this

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div id="main">
        <?php
            //$text = file("C:\\Apache24\\htdocs\\release_notes_2.txt");      // line 10
            $text = array('line 1', 'line 2', 'line 3', 'line 4', 'line 5');  // line 11
            $it = 0;

            echo "<script type='text/javascript'>";                    
            foreach ($text as $line) {
                echo "alert('".$text[$it]."');";
                $it++;
            }
            echo "</script>";
        ?>
    </div>
</body>
</html>

also, to actualy read from file and feed to an array, you need to split all the line manually by processing the file first. set the boundary, like by string count or by ., it is up to you.

lol, its wrong assumption. You need to open the file and read it line by line

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div id="main">
        <?php
            //$text = file("C:\\Apache24\\htdocs\\release_notes_2.txt");      // line 10
            $file = fopen(__DIR__.'/text-2.txt','r'); //the file you wan to read
            
            $text = [];
            if ($file) {
            while (($line = fgets($file)) !== false) {
                $text[] = $line;
            }

            fclose($file);
        } else {
            // error opening the file.
        } 
            
            $it = 0;
            
            echo "<script type='text/javascript'>";                    
            foreach ($text as $line) {
                echo 'alert("'.trim($text[$it]).'");';
                $it++;
            }
            echo "</script>";
            
        ?>
    </div>
</body>
</html>

or

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div id="main">
        <?php
            //$text = file("C:\\Apache24\\htdocs\\release_notes_2.txt");      // line 10
            $file = file(__DIR__.'/text-2.txt');
            
            print_r($file);
            
            $it = 0;
                        
            echo "<script type='text/javascript'>";                    
            foreach ($file as $line) {
                echo 'alert("'.trim(htmlspecialchars($file[$it])).'");';
                $it++;
            }
            echo "</script>";
            
        ?>
    </div>
</body>
</html>
Related