0

I'm trying to temporarily replace all linebreaks with two whitespaces and after some function on the string revert it back from two whitespaces to a linebreak.

But it doesn't work. It won't restore the linebreaks.

This is what I do:

  1. First replace all duplicate whitespaces with a single one.

    $text = preg_replace( '/\s+/', ' ',$text );
    
  2. Replace linebreaks with two whitespaces.

    $text = str_replace( array( '\r', '\r\n', '\n'), '  ', $text );
    
  3. Run some functions..

  4. Restore the linebreaks

    $text = str_replace( '  ', '\n', $text );
    

As far as I can see it replaces the linebreaks with a single whitespace. Not like defined two of them. What happens? Using \s\s doesn't change things.


Tested some things:

str_replace (step 2) fails to detect the linebreaks only AFTER I used preg_replace to replace duplicate whitespaces (step 1).

Without step 1 it works.

Jerry
  • 70,495
  • 13
  • 100
  • 144
user2429266
  • 390
  • 1
  • 3
  • 19
  • You're doing 4 different things here, and apparently one of them doesn't work? Which one? Please remove the other three from the question. Also please post the value of `$text` that is not working for you. – Abhi Beckert Apr 12 '14 at 05:09

3 Answers3

1

I would suggest using those:

  1. $text = preg_replace('/ +/', ' ', $text);

    This will replace only spaces. \s will match more than just spaces, because it also matches vertical whitespaces like linefeeds, carriage returns... and because of this, your second replace wasn't having any newlines to replace.

  2. $text = str_replace(array("\r", "\r\n", "\n"), ' ', $text);

    As hkpotter92 already pointed out, you need to use double quotes, otherwise, you are trying to match the literal characters instead of carriable returns and of line feeds.

  3. $text = str_replace(' ', "\n", $text);

    Once again, you have to use double quotes here, otherwise, you would end up with the literal \n in place of the double spaces.

ideone demo

Community
  • 1
  • 1
Jerry
  • 70,495
  • 13
  • 100
  • 144
  • Your right. I've implemented it this way: `while( strpos( $this->_text, ' ' ) ){ $this->_text = str_replace( ' ', ' ', $this->_text ); }` – user2429266 Apr 12 '14 at 16:49
  • @user2429266 That works too, if you don't mind using a loop :) – Jerry Apr 12 '14 at 16:52
  • But there is another problem. This: `$text = str_replace(array("\r", "\r\n", "\n"), ' ', $text);` replaces every single linebreak twice. Not only once. – user2429266 Apr 12 '14 at 16:54
  • `$this->_text = str_replace( array( "\r", "\r\n", "\n"), ' <!linebreak> ', $this->_text );` Makes this: "text LINEBREAK text LINEBREAK" to: "text <!linebreak> <!linebreak> text <!linebreak> <!linebreak> " – user2429266 Apr 12 '14 at 16:57
  • 1
    @user2429266 If I understand the problem well, that's because of the order. Try using this array instead: `array("\r\n", "\r", "\n")` That way, it will look for `\r\n` before `\r` and single replace breaks. – Jerry Apr 12 '14 at 16:58
0

PHP considers '\n' to be literal \n string and not the newline character. You'd need to use double-quotes (") for the job:

$text = str_replace( array( "\r", "\r\n", "\n"), '  ', $text );
Community
  • 1
  • 1
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • Ok. My bad. But the problem is still there: preg_replace( '/\s+/', 'two whitespaces',$text ) replaces the new lines with a single whitespace instead of two whitespaces. – user2429266 Apr 12 '14 at 05:20
0

In regex \s means whitespace (\n, \r, \t, \f, and " "). That's why it is changing your newlines. Use the space inside the character class [ ] for space only.

$text = preg_replace( '/[ ]+/', ' ',$text );

To change newlines into double spaces, use this one that uses double quotes.

$text = str_replace( array( "\r", "\r\n", "\n"), '  ', $text );

Or, you can use preg_replace for this one too.

$text = preg_replace( '/[\n\r]/', '  ',$text );
Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85