Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP how to convert \n to newline

Tags:

php

How do I make a string that was declared using single quotes, evaluate \n as if it was declared using double quotes?

ie.

echo 'Line1\nLine2'; // Does not split.
echo "Line1\nLine2"; // It splits.

$s = 'A string declared using \n single quotes which I can\'t change...';
echo $s // I need this to have the split at \n
like image 753
Leo Avatar asked Sep 03 '25 03:09

Leo


1 Answers

You should just be able to str_replace them to an actual newline:

$s = str_replace('\n', "\n", $s);

See https://3v4l.org/j0etV

If you're going to be displaying this as HTML, note that you'll also need to run it through nl2br (or if you're using a templating engine this might already be done for you)

like image 104
iainn Avatar answered Sep 06 '25 02:09

iainn