Page 1 of 1

Formating string of html

Posted: Sat Apr 11, 2009 10:30 am
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:

Re: Formating string of html

Posted: Sat Apr 11, 2009 11:12 am
by requinix
Either you write the code yourself or you use something like Tidy.

I think DOMDocument can format output too, actually.

Re: Formating string of html

Posted: Sat Apr 11, 2009 12:35 pm
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.