Formating string of html

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
I'm Steven
Forum Newbie
Posts: 1
Joined: Sat Apr 11, 2009 10:19 am

Formating string of html

Post by I'm Steven »

Howdy,

Kinda new to php and have hit a road block to project completion. please help.

I store html in mysql db as a string, later retrieve html and dump it to screen using htmlentities. Of course it displays as... well, a string. I am trying to format as readable code for users to copy and paste and use for their own use.

example: db returns

Code: Select all

<html><body><h1>whatever</h1><table><tr><td><p>whatever</p></td></tr></table></body></html>
I would like to display as:

Code: Select all

 
<html>
<body>
          <h1>whatever</h1>
          <table>
                    <tr>
                             <td>
                                       <p>whatever</p>
                             </td>
                    </tr>
           </table>
</body>
</html>
 
My app works as intended, but the generated code is not very user friendly. Any suggestions?
Thanks in advance. :banghead:
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Formating string of html

Post by requinix »

Either you write the code yourself or you use something like Tidy.

I think DOMDocument can format output too, actually.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Formating string of html

Post by McInfo »

If you view the output's source code in your browser, does it appear in the readable format? In other words, is it just that the line breaks are not visible? You may need to wrap your string in <pre></pre> tags:

Code: Select all

<pre><?php echo $example_html_from_database; ?></pre>
or replace the newlines with <br>:

Code: Select all

<?php
$example_html = str_replace("\n", "<br>\n", $example_html_from_database);
echo $example_html;
?>
Edit: This post was recovered from search engine cache.
Post Reply