Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php replace PHP_EOL

Tags:

php

The input is from a file. I'm using this code:

$inputText = "a\nb\nc\nd";
$outputText = str_replace(PHP_EOL, ("<br />".PHP_EOL), $inputText);

The output is:

a
<br />b
<br />c
<br />d
<br />

but I need:

a<br />
b<br />
c<br />
d<br />

Can somebody help me pls?

like image 786
Lenny Avatar asked Dec 11 '25 14:12

Lenny


2 Answers

Just use nl2br().

This will do all the work for you. No need for manual replacing.

$outputText = nl2br($inputText);
like image 102
Brad Avatar answered Dec 13 '25 04:12

Brad


Just try with:

$outputText = str_replace("\n", "<br />\n", $inputText) . '<br />';
like image 24
hsz Avatar answered Dec 13 '25 03:12

hsz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!