开发者

Preserving indentation when inserting HTML from MySQL

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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜