How to replace tab with   in PHP?

Roman picture Roman · Jan 18, 2011 · Viewed 27.1k times · Source

In my database I have the following text:

for x in values:
   print x

I want to print this code on my HTML page. It is printed by PHP to the HTML file as it is. But when HTML is displayed by a browser I, of course, do not see text in this form. I see the following:

for x in values: print x

I partially solved the problem by nl2br, I also use str_replace(' ','&nbsp',$str). As a result I got:

for x in values:
print x

But I still need to shift print x to the right. I thought that I can solve the problem by str_replace('\t','   ',$str). But I found out that str_replace does not recognize the space before the print as '\t'. This space is also not recognized as just a space. In other words, I do not get any   before the print.

Why? And how can the problem be solved?

Answer

moinudin picture moinudin · Jan 18, 2011

You need to place \t in double quotes for it to be interpreted as a tab character. Single quoted strings aren't interpreted.