Page 1 of 1

Echo HTML line by line. Possible or not?

Posted: Mon Apr 14, 2008 6:09 am
by digitaldan
Hello,

I've been using the following code to echo lines from a text file before. Now I've tried to use the same thing to echo certain parts of a html page. Instead of converting the text to html, it simply displays the html source as text.

Code: Select all

        <?php
        // file() loads the contents of file.txt into an array, $lines
        // each line in the file becomes a seperate element of the array.
 
        $lines = array_reverse( file( '../pagetest.html' ) );
 
 
        // we can also access each line of the file seperately
 
        echo htmlspecialchars($lines[120]) . '<br />';
        echo htmlspecialchars($lines[121]) . '<br />';
For example, it would echo lines 120 and 121, but instead of displaying the html correctly, it simply echos the html source code.

Any ideas on what I can do to make this script handle the html as html rather than text?

Thanks in advance.

Re: Echo HTML line by line. Possible or not?

Posted: Mon Apr 14, 2008 6:34 am
by onion2k
Did you look up what htmlspecialchars() does?

Re: Echo HTML line by line. Possible or not?

Posted: Mon Apr 14, 2008 6:56 am
by digitaldan
I have changed my code to use the command that translates the html, but it still does not work. I have also removed "array reverse".

Code: Select all

<?php
        // file() loads the contents of file into an array, $lines
        // each line in the file becomes a seperate element of the array.
 
        $lines = file( '../pagetest.html' );
 
 
        // we can also access each line of the file seperately
 
        echo htmlentities($lines[118]) . '<br />';
        echo htmlentities($lines[119]) . '<br />';
        echo htmlentities($lines[120]) . '<br />';
        echo htmlentities($lines[121]) . '<br />';
        echo htmlentities($lines[122]) . '<br />';
Etc.

Still does not convert into html, just shows the html source. It shows:

<table x:str border=0 cellpadding=0 cellspacing=0 width=352 class=xl2731127
style='border-collapse:collapse;table-layout:fixed;width:264pt'>

Etc.

Re: Echo HTML line by line. Possible or not?

Posted: Mon Apr 14, 2008 8:19 am
by onion2k
htmlentities() translates characters that have html entities into their html codes so you can print them without them being displayed as html, just as htmlspecialchars() does only with a wider range of characters. If you want to display the html rather than the text you need to make your code not translate any characters.