Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strip new lines in PHP

Tags:

php

People also ask

How to strip new line in PHP?

'\n' is two characters: a slash followed by the letter 'n'. Double-quoted strings allow escaping of special characters like tabs and newlines.

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.

How do I strip HTML tags in PHP?

The strip_tags() function strips a string from HTML, XML, and PHP tags. Note: HTML comments are always stripped. This cannot be changed with the allow parameter. Note: This function is binary-safe.

How remove all special characters from a string in PHP?

Using str_replace() Method: The str_replace() method is used to remove all the special characters from the given string str by replacing these characters with the white space (” “).


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

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