user580523
user580523

Reputation:

Strip new lines in PHP

I've been working with phrases the past couple of days and the only problem I seem to be facing is stripping new lines in the html before printing it.

Anybody have any idea how to remove every new lines from HTML using PHP?

Upvotes: 34

Views: 43309

Answers (3)

Mukesh Chapagain
Mukesh Chapagain

Reputation: 25948

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

\R matches a generic newline; that is, anything considered a linebreak sequence by Unicode.

https://perldoc.perl.org/perlrebackslash#Misc

\R can be used instead of \r\n.

Upvotes: 0

user319730
user319730

Reputation: 71

the pt2ph8 answer didn't work for me until I have changed it a little bit as follows:

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

You can see the different results in this demo:

http://phpfiddle.org/lite/code/g5s8-nt3i

Upvotes: 2

ySgPjx
ySgPjx

Reputation: 10245

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

Upvotes: 81

Related Questions