Using nl2br() in PHP

623 views Asked by At

I am using below code.

<?
echo nl2br( 'The situation deteriorated into attacks on New York elite, followed by attacks on black New Yorkers and their property after fierce competition for a decade between 
Irish immigrants and black people for work. Rioters burned the Colored Orphan Asylum to the ground.' , false  ) ;
?>

I am getting output like below

The situation deteriorated into attacks on New York elite, followed by attacks on black New Yorkers and their property after fierce competition for a decade between <br>
Irish immigrants and black people for work. Rioters burned the Colored Orphan Asylum to the ground.

I would like to get output without <br> but I need a line break (New Line) at that point.

2

There are 2 answers

5
Rick On

Why can't you just do this..

<?
echo "The situation deteriorated into attacks on New York elite, followed by attacks on black New Yorkers and their property after fierce competition for a decade between\n Irish immigrants and black people for work. Rioters burned the Colored Orphan Asylum to the ground.";
?>
0
AudioBubble On

Try this one. Btw I do not know what is with the "false" behind the string but if you remove it in your code it works just fine and I see no html break line tag.

<?php 
$string = 'The situation deteriorated into attacks on New York elite,followed by attacks on black New Yorkers and their property after fierce competition for a decade between Irish immigrants and black people for work. Rioters burned the Colored Orphan Asylum to the ground.';
function nl2br2($string) { 
$string = str_replace(array("\r\n", "\r", "\n"), "<br />", $string); 
return $string; 
} 
echo"$string";
?>