PHP has a function nl2br for converting newlines to html <br/>. This function is really useful when asking for user comments / feedback, because by using
<?php
$comment = nl2br(strip_tags($comment));
?>
we can remove the unwanted html tags and at the same time retain the line breaks. But the function does not actually replace the "\n" with <br/> but with "\n<br/>". Hence you may run intro trouble, if you save the data in flat file. Also there is no equivalent br2nl.
So here is my functions nl2break, which converts "\n" to "<br/>" and break2nl, which converts <br/> back to "\n". Also included is a function nl2para, that converts two consecutive newlines to a paragraph and a single newline to break.
<?php
function nl2break($str){
return preg_replace('#\r?\n#', '<br />', $str);
}
function break2nl($str){
return preg_replace('/\<br\s*\/?\>/i', "\n", $str);
}