tags:

views:

44

answers:

4

I have to render some text to a web page. The text is coming from sources outside my control and it is formatted using \n and\t

Now \n can be replaced by a <br> but what about \t. A brief search reveals there is no way to render tab characters in html, bu there has to be a workaround. Anyone?

+2  A: 
replace \t with &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

Each space you want will be a &nbsp;

Rippo
+2  A: 

Why not just wrap the content in a <pre> tag? This will handle the \n as well as the \t characters.

Matt
this breaks the rest of my layout pretty bad
Midhat
+2  A: 

If you're already replacing line breaks, why not do the same for tabs...?

str_replace("\t", '&nbsp;&nbsp;&nbsp;&nbsp;', $text);
deceze
A: 

An alternative to the non-breaking space would be the em space (&emsp; or &#x2003;). It is usually rendered as a longer space, if that is an advantage.