How can I put in HTML a string with spaces?(PHP)?

I want to take from a MySQL database a text and put it with PHP in HTML syntaxes.

The problem is that if I use ~echo $string;~ will not appear spaces and indentations, because that is the way how HTML works. If I use ~echo "<pre>".$string."</pre>";~ I am getting spaces and indentations, but I am loosing the HTML codes from string.

Does anybody know a way to not lose HTML codes from string and also to have spaces?

Thank you very much.

And I'm sorry for my language, I'm not a native English speaker.

2 Answers

  • 10 years ago
    Favorite Answer

    Spaces and indentations should appear just fine. Are you talking about having two spaces look the same as one? That's part of HTML. You should never, ever have a string that's "sort of" HTML. If it has HTML codes in it, it must be HTML, and then two spaces must mean the same thing as one space. If it handles spacing differently from HTML, it's not HTML.

    Trust me, I'm speaking from years of experience. You will never get a "sort of" HTML to work the way you want it to. Either make the string HTML (containing HTML codes but where two spaces mean the same thing as one space) or make it not HTML (not containing HTML codes).

    If you're trying to use spaces or tabs to line things up, instead use appropriate HTML codes (such as tables or non-breaking spaces) to line things up.

    • Commenter avatarLogin to reply the answers
  • 10 years ago

    You can use   (non-breaking space). But unless you use a monospaced font line (maybe) in <tt> or <pre> the columns won't line up.

    Better to use <table> with <tr><td>xx</td></tr> etc.

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.