Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace \n with actual new line character code

Tags:

php

newline

utf-8

I'm pulling content from a DB that has been sanitized using mysql_real_escape_string. Accordingly the new line characters now appear as "\n". The issue is that this content is displayed to users inside a < pre > tag so I cannot replace \n with < br/> for instance.

I suppose I could replace \n with the actual utf8 character code before inserting the result inside the < pre>.

Can somoneone assist here? Not using mysql_real_escape_string isn't really an option due to security policy.

Thanks.

like image 512
Max Avatar asked Jul 16 '10 11:07

Max


People also ask

Does \n make a new line?

The newline character ( \n ) is called an escape sequence, and it forces the cursor to change its position to the beginning of the next line on the screen. This results in a new line.

How do you replace a character with a new line in VS code?

Today I learned How to find and replace with a newline in Visual Studio Code. In the local searchbox ( Ctrl + F ) you can insert newlines by pressing Ctrl + Enter . If you use the global search ( Ctrl + Shift + F ) you can insert newlines by pressing Shift + Enter .

How do you convert N to a new line in Python?

It can be None, '', '\n', '\r', and '\r\n'. It works as follows: On input, if newline is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller.


4 Answers

echo '<pre>'.str_replace('\n', "\n", $string).'</pre>';
like image 155
Mark Baker Avatar answered Oct 03 '22 00:10

Mark Baker


str_replace("\\n","\n",$data);
like image 45
R. Hill Avatar answered Oct 02 '22 23:10

R. Hill


why not ...

echo str_replace('\\n', PHP_EOL, "few lines\nof text here,\nblah, blah! etc!");
like image 6
Lou Avatar answered Oct 03 '22 00:10

Lou


I'm pulling content from a DB that has been sanitized using mysql_real_escape_string. Accordingly the new line characters now appear as "\n"

If you've not done anything to the raw data pulled back from the database then the ptoblem is that it has been 'sanitized' twice when inserted.

C.

like image 1
symcbean Avatar answered Oct 03 '22 00:10

symcbean