tags:

views:

119

answers:

8
$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 ";
+2  A: 

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

NullUserException
OP wants a space between "paragraphs" and "for". If you just remove the newline characters, there won't be one.
Daniel Vandersluis
Please revoke your minus as my post has been updated.
fabrik
+1, `[\r\n\]+` would be even better
stereofrog
or simply replace `\s+ => ' '`
stereofrog
first one doesn't work
Happy
please correct that to `$string = preg_replace("~[\r\n]~", "",$string);`
erikb
Otherwise there is nothing more to say. that is the most simple answer, I think!
erikb
@erik Then I would be "incorrecting" it. It works fine as it is.
NullUserException
@Downvoters Explain
NullUserException
@Daniel I got the idea that the OP wanted to remove all newline characters, period.
NullUserException
Now you already changed it and I think its fine. When I loaded your comment it didn't include your edit. Btw. the second code example still misses a semicolon. edit: ah, now I know what you mean. Yes I forgot that php treats " and ' differently
erikb
@Null I'm just going by the example output (I didn't downvote though).
Daniel Vandersluis
Your script fails because it's return: "put returns between paragraphsfor linebreak add 2 spaces at end" See it live @ http://ideone.com/ugNOZ
fabrik
@fabrik The OP said "Want to remove all new lines." Well, that's exactly what it does.
NullUserException
Why don't you read the whole question itself? (hint: $string should become:...)
fabrik
+7  A: 
elusive
+1 for the regex
Daniel Vandersluis
Before you use this solution, see my answer below and the comments to my answer. This solution may not work as you expect.
matsolof
+1  A: 
$string = str_replace(array("\n", "\r"), ' ', $string);
fabrik
Escape sequences in PHP need to be double quoted.
Daniel Vandersluis
This doesn't do exactly what the OP wants though, since this is the same as elusive's answer and it's not removing the `\r` characters
NullUserException
@NullUser: Why don't you update your own post instead of commenting on this?
fabrik
Because my answer is already correct (AFAIK)
NullUserException
@NullUser: Except the fact it's replaces everything with '' instead of ' '.
fabrik
+1  A: 

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 ''.

ianhales
The `preg` functions use PCRE, not POSIX, regular expressions. There are `ereg` functions that use POSIX, but they are now deprecated.
Daniel Vandersluis
Oops, good point, sorry! Correcting now.
ianhales
+1  A: 

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);
netom
Please format your code properly. This is done by indenting the relevant line by four spaces.
elusive
That's why I use single quotes so I don't need to escape those characters. `\\n` looks terrible. `\\\\ ` to match a single backslash is just awful.
NullUserException
+1  A: 

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

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

this is the pattern I would use

$string = preg_replace('@[\s]{2,}@',' ',$string);
regexhacks
+1  A: 

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));
matsolof
You're right; `/\s\s+/` would fail to match the linefeed in `foo\nbar`.
Alan Moore
Good to get a cofirmation that I'm right. What do "foo" and "bar" mean, by the way? I know they're placeholders, but what do the words actually mean? And wouldn't it be better to use more familiar words, that would be less confusing to beginners, as placeholders? When I first saw "foo" and "bar" in a tutorial or another, I thought the words were code, and it took me a while to figure out they aren't.
matsolof