Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape new line from string

Tags:

php

is there any php method to remove new line char from string?

$str ="
Hi
there
";

my string contains a new line char between 'Hi' and 'there' i want output as a "Hi there".I don't want to use regular expression.

like image 710
user370527 Avatar asked Jul 08 '10 01:07

user370527


People also ask

How do you escape a new line in Python?

In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.

How do you escape a new line in HTML?

Escape the HTML, and then replace \n with <br> .


1 Answers

This is a bit confusing

is there any php method to remove new line char from string?

It looks like you actually want them replaced with a space.

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

Assuming the replacing goes from left to right, this should suit Windows text files.

The first grouping is to match Windows newlines which use both \r and \n.

like image 189
alex Avatar answered Oct 11 '22 17:10

alex