0

i want to know how to keep all whitespaces of a text area in php (for send to database), and then echo then back later. I want to do it like stackoverflow does, for codes, which is the best approach?

For now i using this:

$text = str_replace(' ', '&nbs p;', $text);

It keeps the ' ' whitespaces but i won't have tested it with mysql_real_escape and other "inject prevent" methods together.

For better understanding, i want to echo later from db something like:

 function jack(){
    var x = "blablabla";
 }

Thanks for your time.

5
  • 1. Store the data as is 2. rightclick the code block of you question and choose "inspect element" 3. ? 4. profit! Commented Sep 28, 2013 at 16:44
  • But if i use mysql_real_escape and do a line break with 5 white spaces on second line, it will echo just one, or none. How the proper way to echo it back using the white space char code? Or should i save then on db already using the whitespace code? Commented Sep 28, 2013 at 16:48
  • 1
    Read my comment and not just the 5 first words. The whitespace is there, but the user agent (the browser) just doesn't display it. Also stop using the deprecated mysql_* functions, because they will be removed from the language soon. Commented Sep 28, 2013 at 16:50
  • 1
    You wont answer a question saying "1 2 3" its done. Please, if you wont want to help, let someway that want and dont do offensive comments. What i wanted is the better approach, the safer, for code blocks. Commented Sep 28, 2013 at 17:00
  • 1. It isn't an answer, but a comment. 2. It actually answers your question. 3. ? 4. I fail to see how any of my comments where offensive? Commented Sep 28, 2013 at 17:04

2 Answers 2

1

Code Blocks

If you're trying to just recreate code blocks like:

function test($param){
    return TRUE;
}

Then you should be using <pre></pre> tags in your html:

<pre>
    function test($param){
        return TRUE;
    }
</pre>

As plain html will only show one space even if multiple spaces/newlines/tabs are present. Inside of pre tags spaces will be shown as is.

At the moment your html will look something like this:

function test($param){
&nbsp;&nbsp;&nbsp;&nbsp;return TRUE;
}

Which I would suggest isn't desirable...

Escaping

When you use mysql_real_escape you will convert newlines to plain text \n or \r\n. This means that your code would output something like:

function test($param){\n return TRUE;\n}

OR

<pre>function test($param){\n    return TRUE;\n}</pre>

To get around this you have to replace the \n or \r\n strings to newline characters.

Assuming that you're going to use pre tags:

echo preg_replace('#(\\\r\\\n|\\\n)#', "\n", $escapedString);

If you want to switch to html line breaks instead you'd have to switch "\n" to <br />. If this were the case you'd also want to switch out space characters with &nbsp; - I suggest using the pre tags.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Steven. I was wondering if i can use mysql_real_escape with the pre tags and you answered that.
1

try this, works excellently

$string = nl2br(str_replace(" ", " &nbsp;", $string));
echo "$string";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.