How to avoid \n and \r in PHP using nl2br

502 Views Asked by At

I am trying to avoid the \n and \r values from the mysql table using nl2br as shown below

$profileDetails['address']=nl2br($profileDetails['address']);

But it is not working.

the value in $profileDetails['address'] is "Naduvilachirayil House,\r\nAnaprampal North P.O,\r\nThalavady".

But when I check,

echo nl2br("Naduvilachirayil House,\r\nAnaprampal North P.O,\r\nThalavady");

It is working..

1

There are 1 best solutions below

4
On

nl2br does not convert the string \n into a <br>. It converts new lines into a <br>. To show the difference:

"\n" === "
";

$profileDetails['address'] === "Naduvilachirayil House,\\r\\nAnaprampal North P.O,\\r\\nThalavady";

That is to say, the value from your database is a literal \ character followed by an n character. \n only means "new line" when the string is being defined.