Any transformation for the output medium (that includes any HTML escaping or converting newlines to HTML) should happen at the latest possible moment, preferably right as you’re outputing to the HTML page.
It may also be helpful to know that the nl2br function doesn’t actually convert a newline character to a <br>. It simply inserts the <br> characters into the string (prior to the newline character), leaving the newline character intact (helpful to know especially e.g. when output is to be interpreted as a javascript string).