Php remove newline from string

$string = "
put returns between paragraphs

for linebreak add 2 spaces at end

";

Want to remove all new lines from string.

I've this regex, it can catch all of them, the problem is I don't know with which function should I use it.

/\r\n|\r|\n/

$string should become:

$string = "put returns between paragraphs for linebreak add 2 spaces at end ";

Php remove newline from string

Marco Demaio

32.6k33 gold badges126 silver badges157 bronze badges

asked Sep 21, 2010 at 13:34

2

You have to be cautious of double line breaks, which would cause double spaces. Use this really efficient regular expression:

$string = trim(preg_replace('/\s\s+/', ' ', $string));

Multiple spaces and newlines are replaced with a single space.

Edit: As others have pointed out, this solution has issues matching single newlines in between words. This is not present in the example, but one can easily see how that situation could occur. An alternative is to do the following:

$string = trim(preg_replace('/\s+/', ' ', $string));

answered Sep 21, 2010 at 13:36

jwuellerjwueller

30k4 gold badges65 silver badges70 bronze badges

5

A few comments on the accepted answer:

The + means "1 or more". I don't think you need to repeat \s. I think you can simply write '/\s+/'.

Also, if you want to remove whitespace first and last in the string, add trim.

With these modifications, the code would be:

$string = preg_replace('/\s+/', ' ', trim($string));

answered Sep 22, 2010 at 15:20

matsolofmatsolof

2,5553 gold badges17 silver badges17 bronze badges

1

Just use preg_replace()

$string = preg_replace('~[\r\n]+~', '', $string);

You could get away with str_replace() on this one, although the code doesn't look as clean:

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

See it live on ideone

answered Sep 21, 2010 at 13:36

NullUserExceptionNullUserException

82k27 gold badges205 silver badges230 bronze badges

13

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

answered Sep 21, 2010 at 13:36

Php remove newline from string

fabrikfabrik

13.8k8 gold badges56 silver badges70 bronze badges

4

You should use str_replace for its speed and using double quotes with an array

str_replace(array("\r\n","\r"),"",$string);

answered Sep 21, 2010 at 13:39

RobertPittRobertPitt

56.2k21 gold badges113 silver badges159 bronze badges

3

I'm not sure if this has any value against the already submitted answers but I can just as well post it.

// Create an array with the values you want to replace
$searches = array("\r", "\n", "\r\n");

// Replace the line breaks with a space
$string = str_replace($searches, " ", $string);

// Replace multiple spaces with one
$output = preg_replace('!\s+!', ' ', $string);

Jimmix

4,9543 gold badges32 silver badges61 bronze badges

answered Nov 19, 2015 at 13:48

Php remove newline from string

PeterPeter

8,3396 gold badges55 silver badges92 bronze badges

Escape sequence \R matches a generic newline

that is, anything considered a linebreak sequence by Unicode. This includes all characters matched by \v (vertical whitespace), and the multi character sequence \x0D\x0A...

$string = preg_replace('/\R+/', " ", $string);

In 8-bit non-UTF-8 mode \R is equivalent to the following: (?>\r\n|\n|\x0b|\f|\r|\x85)... pcre.org

Regex101 Demo

Php remove newline from string

answered Nov 16, 2014 at 12:39

AntonAnton

1291 silver badge1 bronze badge

1

Line breaks in text are generally represented as:

\r\n - on a windows computer

\r - on an Apple computer

\n - on Linux

//Removes all 3 types of line breaks

$string = str_replace("\r", "", $string);

$string = str_replace("\n", "", $string);

Dave

3,0667 gold badges19 silver badges33 bronze badges

answered Aug 2, 2018 at 12:08

Php remove newline from string

PCRE regex replacements can be done using preg_replace: http://php.net/manual/en/function.preg-replace.php

$new_string = preg_replace("/\r\n|\r|\n/", ' ', $old_string);

Would replace new line or return characters with a space. If you don't want anything to replace them, change the 2nd argument to ''.

answered Sep 21, 2010 at 13:37

Php remove newline from string

n00dlen00dle

5,8112 gold badges35 silver badges48 bronze badges

1

Use this:

replace series of newlines with an empty string:

$string = preg_replace("/[\\n\\r]+/", "", $string);

or you probably want to replace newlines with a single space:

$string = preg_replace("/[\\n\\r]+/", " ", $string);

answered Sep 21, 2010 at 13:37

netomnetom

3,2722 gold badges20 silver badges21 bronze badges

2

Whats about:

$string = trim( str_replace( PHP_EOL, ' ', $string ) );

This should be a pretty robust solution because \n doesn't work correctly in all systems if i'm not wrong ...

answered Jun 8, 2016 at 11:45

GDYGDY

2,7451 gold badge23 silver badges42 bronze badges

3

I was surprised to see how little everyone knows about regex.

Strip newlines in php is

$str = preg_replace('/\r?\n$/', ' ', $str);

In perl

$str =~ s/\r?\n$/ /g;

Meaning replace any newline character at the end of the line (for efficiency) - optionally preceded by a carriage return - with a space.

\n or \015 is newline. \r or \012 is carriage return. ? in regex means match 1 or zero of the previous character. $ in regex means match end of line.

The original and best regex reference is perldoc perlre, every coder should know this doc pretty well: http://perldoc.perl.org/perlre.html Note not all features are supported by all languages.

answered Feb 10, 2017 at 13:54

ekernerekerner

5,5131 gold badge36 silver badges31 bronze badges

2

Many of these solutions didn't work for me. This did the trick though:-

$svgxml = preg_replace("/(*BSR_ANYCRLF)\R/",'',$svgxml);

Here is the reference:- PCRE and New Lines

answered Apr 8, 2019 at 16:22

2

this is the pattern I would use

$string = preg_replace('@[\s]{2,}@',' ',$string);

answered Sep 22, 2010 at 7:10

RajeevRajeev

4,2812 gold badges21 silver badges35 bronze badges

This one also removes tabs

$string = preg_replace('~[\r\n\t]+~', '', $text);

answered May 14, 2015 at 2:23

You can try below code will preserve any white-space and new lines in your text.

$str = "
put returns between paragraphs

for linebreak add 2 spaces at end

";

echo preg_replace( "/\r|\n/", "", $str );

answered Nov 9, 2016 at 11:00

Php remove newline from string

maybe this works:

$str='\n';
echo str_replace('\n','',$str);

answered Apr 22, 2017 at 15:26

Php remove newline from string

زيادزياد

89210 silver badges14 bronze badges

You can remove new line and multiple white spaces.

$pattern = '~[\r\n\s?]+~';
$name="test1 /
                     test1";
$name = preg_replace( $pattern, "$1 $2",$name);

echo $name;

answered Jun 22, 2019 at 7:19

Php remove newline from string

RameshRamesh

1,35111 silver badges14 bronze badges

Very simple

$hello = "
A
B
C
";
str_replace("
", " ", $hello);
// A B C

answered Nov 5, 2021 at 6:55

Php remove newline from string

MincoMinco

562 bronze badges

this below code work all text please use it:

$des = str_replace('\n',' ',$des);
$des = str_replace('\r',' ',$des);

answered Jun 15, 2018 at 7:26

Using a combination of solutions mentioned above, this one line worked for me

 $string = trim(str_replace('\n', '', (str_replace('\r', '', $string))));

It removes '\r' and '\n'.

answered Jun 9, 2021 at 5:43

Not the answer you're looking for? Browse other questions tagged php regex string or ask your own question.

How remove all new lines from a string in PHP?

The line break can be removed from string by using str_replace() function.

Does PHP trim Remove newline?

Yes it does, see the manual: This function returns a string with whitespace stripped from the beginning and end of str .

How do I remove a newline from the end of a string?

Use the trim() method to remove the line breaks from the start and end of a string, e.g. str. trim() . The trim method removes any leading or trailing whitespace from a string, including spaces, tabs and all line breaks.

Can we use \n in PHP?

?> Using new line tags: Newline characters \n or \r\n can be used to create a new line inside the source code.