Keeping indentation when pasting HTML from MySQL

I use MySQL and PHP to populate parts of a site, often with HTML stored in a TEXT field. I like to have my HTML indented so that the source is neat and easy to read, e.g .:

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

      

However, when HTML is pulled from MySQL, I get:

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

      

It's pretty ugly when a lot of HTML is added to the DIV, which is significantly indented. How can I stop this? FYI, I am using wordwrap () so that each line is not too long.

+2


a source to share


2 answers


You can defer 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) .

one if you don't want the first line to be indented.

+2


a source


What type of data do you store HTML in the database with? I suspect you are using VARCHAR, but BLOB would be better.



An easy fix is ​​to use spaces, not tabs, in HTML. The database seems to be ignoring the \ t character.

0


a source







All Articles