2

How do I remove multiple occurrences of \n from the sample below and replace with just one occurrence of \n?

Basically I just want to remove multiple line breaks and replace them with just one line break.

\n\n\n\n\n\n\n\n \n\n \n\n\n\n\n\n\n    \n\n     \n\n     \n\n    \n\n    \n\n     \n\n     \n\n     \n     \n     \n     \n     \n     \n     \n\n     \n\n     \n\n     \nEDITION:  U.S.\n\n \nINTERNATIONAL\n\n     \nMÉXICO\n\n     \n\n     \nSet edition preference\n\n     \n\n     \n\n     \n\n     \nSign up\n\n     \nLog in\n\n     \n\n \n\n     \n\n     \n\n     \n\n\n\n\n\n\n\n\n\n\n\n     \n\n     \n\n\n \n\n \n\n     \n\n     \n\n    \n\n    \n\n     \n\n     \nHome\n\n     \nVideo\n\n     \nNewsPulse\n\n \nU.S.\n\n     \nWorld\n\n     \nPolitics\n\n     \nJustice\n\n     \nEntertainment\n\n     \nTech\n\n     \nHealth\n\n     \nLiving\n\n     \nTravel\n\n \nOpinion\n\n     \niReport\n\n     \nMoney\n\n     \nSports\n\n     \n\n    \n\n\n\n\n\n \n\n\n\n\n\n\n\n \n\n\n\n \n\n\n\n\n\n\n \n\nupdated 10:02 a.m.EDT, Fri June 3, 2011\n\n\n\n\n\n \n\n\n\n\n\nDr. Jack Kevorkian dead at 83\n\n\n\n\n\n\nThe Michigan pathologist who put assisted suicide on the world\'s medical ethics stage, apparently died of a blood clot, according to his attorney. FULL STORY
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dylan
  • 33
  • 1
  • 2
  • 5

7 Answers7

3

Two ways

while(strpos($string, "\n\n") !== false)
  str_replace("\n\n", "\n", $string);

And

preg_replace("/\n+/", "\n", $string);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dynamic
  • 46,985
  • 55
  • 154
  • 231
  • In my framework I am currently using hte first version because when I wrote it 6 years ago didn't know much about regex XD (I was kiddish too) – dynamic Jun 03 '11 at 15:35
2

This should work:

<?php
$string = "\n\n\n\n Text \n\n Text \n\n\n\n\n Text \n\n\n";

echo preg_replace("#[\n]+#", "\n", $string);
Jim
  • 18,673
  • 5
  • 49
  • 65
1

If this is a real carriage return you can do this to remove successive carriage returns:

preg_replace('/\n+/', '\n', $yourString);

Else for the string '\n' you can do:

preg_replace('/(\\n)+/', '\n', $yourString);

Finally, if you want to remove all spaces in between your \n too you can do"

preg_replace('/\s*\n+/', '\n', $yourString);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
malko
  • 2,292
  • 18
  • 26
  • 1
    For a linebreak to work, you need to enclose it in double quotes on at least the replaced part, IE `"\n"`. – Jim Jun 03 '11 at 14:39
  • The `\n` in the single quoted regex are correct (more advisable to have PCRE interpret the escape sequences). But the replacement string must indeed be filled by PHP with the correct character sequences. – mario Jun 03 '11 at 14:42
  • only for the second case where we want to replace string '\n' we should keep the replacement part as single quote, others require double quotes. – malko Jun 03 '11 at 14:44
1

Try forcing the + match to be greedy, by using ++ instead.

preg_replace('/\n++/', "\n", $yourString);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lethargy
  • 1,859
  • 14
  • 15
  • Problem you will have is you need `"` around the replacement `\n` for it to return a new line character. – Jim Jun 03 '11 at 14:46
  • Oh whoops. Sorry I Just copied it from another example without properly looking :P – Lethargy Jun 03 '11 at 14:58
0

Strange, none of the code works? Example:

$barcodes = "5312353123123



5312353123123



5312353123123";
echo(     var_dump(   $barcodes     )     . '</br>' . "\n"  );
$barcodes = preg_replace('/\n+/', "\n", $barcodes);
exit(  var_dump(   $barcodes     )     . '</br>' . "\n"  );

Output:

string(55) "5312353123123 5312353123123 5312353123123"
string(55) "5312353123123 5312353123123 5312353123123"

That means the function does... nothing?

Yoong Kim
  • 310
  • 1
  • 6
  • 13
0

Another way from the gotcha's examples on the man page for str_replace() is:

// Order of replacement
$str     = "Line 1\nLine 2\rLine 3\r\nLine 4\n";
$order   = array("\r\n", "\n", "\r");
$replace = '<br />';

// Processes \r\n's first so they aren't converted twice.
$newstr = str_replace($order, $replace, $str);
Sanford Staab
  • 239
  • 2
  • 10
  • FYI: `\R` is much more compact than `array("\r\n", "\n", "\r")` and have the same meaning. – Toto Feb 19 '14 at 17:47
0

Try:

$newstr = preg_replace("/\r\n\r\n|\r\r|\n\n/", "..", $str);
kenorb
  • 155,785
  • 88
  • 678
  • 743
tashi
  • 249
  • 1
  • 3
  • 16