-4

I can convert the "newlines" to the HTML line breaks using nl2br($test) command, but I want the command to convert all the newlines except the last one. I don't want any breaks after the last line. Could you please help me on this?

someOne
  • 1,975
  • 2
  • 14
  • 20

1 Answers1

0

You can use this example:

When you convert your string with nl2br and the function adds a <br /> at the end of the resulting string; then you can just eliminate the last <br /> with either substr or rtrim functions:

<?php
$string = "This\r\nis\n\ra\n\rtest\n\r";
$string = trim(nl2br($string));


echo $string;
echo "<br>\n";
echo substr($string, 0, -6);
echo "<br>\n";
echo rtrim($string, '<br />');

?>
someOne
  • 1,975
  • 2
  • 14
  • 20
Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63