-1

I have following exact string stored in database:

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an                           unknown printer took a galley of type and scrambled it                      to make a type specimen book. 


It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Notice the extra spaces and extra paragraph / newline I have added in the string.

In php I am showing this data using echo nl2br($val); then it preserves the paragraph newline but removes the extra spaces given in the string.

How I can fix this, please help!

axiac
  • 68,258
  • 9
  • 99
  • 134
djmzfKnm
  • 26,679
  • 70
  • 166
  • 227
  • See: http://stackoverflow.com/q/2300142/3933332 – Rizier123 Nov 09 '16 at 10:21
  • 2
    This really has much more to do with [tag:html] than [tag:php]… – deceze Nov 09 '16 at 10:22
  • 1
    The real question is why you want to do this -- if you're trying to format your text by putting blocks of spaces into it, then you're doing it wrong. With all the formatting features available in HTML and CSS, there are very few good reasons to try to do it with space characters. The example you've given is obviously not an accurate representation of what you're trying to achieve, so it's hard for me to go further and say how you should do it, but spaces isn't the answer. – Simba Nov 09 '16 at 10:58

4 Answers4

1

PHP does not remove the spaces. Not even HTML does.

But HTML compacts two or more consecutive whitespaces to a single one on rendering. Because this is how HTML works.

There are several ways to force it render the text as is.

One of them is to put the text into a <pre> element:

echo '<pre>', $val, '</pre>';  // there is no need to nl2br()

Another option is to force the element rendering using the CSS attribute white-space:

echo '<p style="white-space: pre;">', $val, '</p>';
deceze
  • 510,633
  • 85
  • 743
  • 889
axiac
  • 68,258
  • 9
  • 99
  • 134
  • @deceze What's wrong using `echo()` like a function? (apart from the performance penalty of concatenating strings, that doesn't count so much most of the times...) – axiac Nov 09 '16 at 11:15
  • Nothing *wrong* with it per se, but it's superfluous. It's like adding superfluous parentheses around every expression (`foo((((2+2))))`). It doesn't do anything, and should therefore not be in a supposedly canonical code sample. – deceze Nov 09 '16 at 11:49
  • I don't agree. Do you also remove the curly brackets from conditional statements and loops when they contain only one statement? The curly brackets in such a context are superfluous and don't do anything, isn't it? – axiac Nov 09 '16 at 12:35
  • Would you agree with the [PSR](http://www.php-fig.org/psr/) standards or the [PHP manual on `echo`](http://php.net/echo)? Both use `{}` brackets but omit `()` parentheses on `echo`. – deceze Nov 09 '16 at 13:04
  • Both the PSR standards (which I agree with) and the optional parentheses on `echo` are a matter of preference. PSR-2 provides a good reason to use those *superfluous* braces: *"This standardizes how the structures look, and reduces the likelihood of introducing errors as new lines get added to the body."*. I can also provide you a good reason to use `echo` with parentheses: it looks like a function, (in that context) it behaves like a function, it is even listed under "String functions" in the manual. `print()`, which is also *not a function* even returns a value. And it's more readable, btw. – axiac Nov 09 '16 at 13:32
  • Au contraire, [the manual](http://php.net/echo) explicitly states: *"`echo` is not actually a function [..], so you are not required to use parentheses with it. `echo` [..] does not behave like a function, so it cannot always be used in the context of a function. [..] if you want to pass more than one parameter to `echo`, the parameters __must not__ be enclosed within parentheses."* – deceze Nov 09 '16 at 13:34
  • Contrary to that, `print` actually *does* behave more like a function in that it has a return value and can be used as part of an expression. – deceze Nov 09 '16 at 13:35
  • I'll leave it at this as far as my opinion and evidence goes. If you still disagree, feel free to roll back the edit. – deceze Nov 09 '16 at 13:37
0

Use <pre><?= $val; ?></pre> tag wrapper to preserve any white-space and new lines in your text.

Justinas
  • 41,402
  • 5
  • 66
  • 96
0

The spaces are closed up due to the way HTML works.

The best ways to preserve them are:

  1. Replace the spaces with &nbsp;.
    This is a HTML entity for a 'non-breaking space' character. It shows up as a space, but is treated as a printable character by the browser. It therefore does not get closed up the way normal spaces are. It also prevents the browser from word-wrapping at that point, so it's useful if you want to force words to be together.

  2. Use CSS white-space: pre;.
    This forces the browser to stop closing up all spaces. It also brings back the carriage returns in your text, so it does pretty much what you're asking for. It can have knock-on effects on your page layout though.

Either of these will do the trick. However, I would re-iterate what I said in the comments above -- using spaces to manipulate the formatting of your text is almost always the wrong solution. It might have been the right thing to do in some cases fifteen years ago when IE6 was king and we didn't have as many CSS features available to play with, but today it's almost certainly not the right approach.

Simba
  • 4,952
  • 3
  • 19
  • 29
-1

Let's try below code will preserve any white-space and new lines in your text.

$str = "<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an                           unknown printer took a galley of type and scrambled it                      to make a type specimen book.</p>

<p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>";

echo preg_replace( "/\r|\n/", "", $str );
PCMShaper
  • 54
  • 5