Echo HTML line by line. Possible or not?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
digitaldan
Forum Newbie
Posts: 9
Joined: Wed Oct 17, 2007 4:04 am

Echo HTML line by line. Possible or not?

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

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

Post by onion2k »

Did you look up what htmlspecialchars() does?
digitaldan
Forum Newbie
Posts: 9
Joined: Wed Oct 17, 2007 4:04 am

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

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

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

Post 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.
Post Reply