views:

93

answers:

2

I am using MySQL and PHP to populate parts of a site, often with HTML stored in a TEXT field. I like to keep my HTML indented so that the source is neat and easy to read, for example:

<body>
   <div>
      <p>Blahblah</p>
   </div>
</body>

However, when the HTML is pulled from MySQL, I end up with:

<body>
   <div>
<p>Blahblahblah</p>
   </div>
</body>

This is quite ugly when there is a large amount of HTML being inserted into a DIV that is significantly indented. How can I stop this from happening? FYI, I use wordwrap() to keep each line from being too long.

A: 

With which datatype are you storing the HTML in the database? I suspect you are using VARCHAR, but BLOB would be better.

A simple fix would be to use spaces rather than tabs in the HTML. It seems the database is ignoring the \t character.

jeph perro
+2  A: 

You could indent the lines manually with PHP:

$tabs = 4;
echo str_repeat(chr(9), $tabs) . str_replace(chr(10), chr(10) . str_repeat(chr(9), $tabs), $text);

You can also remove the first str_repeat(chr(9), $tabs) . if you don't want the first line to get indented.

Dragory
Brilliant! Got it to do exactly what I wanted it to do by passing $text through a wordwrap() first, then through your solution, and finally echoing $text in the DIV. As for the folks who were arguing above, why not do this? It makes me happy LOL Thanks, Dragory!
Benjamin