0

I just want to replace "\n" characters to new line in a string using php, like that :

string 'foo\n\nbar'

to

string 'foo

bar`

Anybody have an idea ? Thanks.

0

2 Answers 2

3

use str_replace('\n', PHP_EOL, 'foo\n\nbar');

<?php
header('Content-Type: text/plain');

$string = 'foo\n\nbar';

$string = str_replace('\n', PHP_EOL, $string);

echo $string;
?>

Shows:

foo

bar
Sign up to request clarification or add additional context in comments.

3 Comments

would be cool if you add nl2br option to your answer in the case that the text is to be displayed as HTML
@Okeke Emmanuel Oluchukwu, nl2br is already mentioned by chandresh_cool.
Oh my, really sorry about that @CORRUPT
1

you can always Use

nl2br

function

1 Comment

It seems that the answer is replace the \n by PHP_EOL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.