How to convert \n \r ‘line breaks’ to HTML <br> line breaks

The nl2br PHP function can be used to convert \n \r ‘line breaks’ to HTML <br> line breaks.

It will correctly handle the variations of

  • \r\n
  • \n\r
  • \n
  • \r

How do I use it?

For example:

<?php
echo nl2br( "Welcome to\nWordPress" );
?>

Will output

Welcome to
WordPress