As any student of computer science knows, line breaks are confusing. There are styles of line breaks unique to Unix, Mac and Windows – so what should a web renderer do when faced with a newline command? In HTML, it’s simple, they should be ignored. But what when it is user generated text, not HTML?
This was a problem I faced when trying to get Dabr to render the ASCII* art produced by Aral Balkan‘s Feathers App. Feathers uses line breaks to achieve images like…
〰❀❃ Introducing ❃❀〰
My new iPhone app,
░░░▒█ Feathers █▒░░░
What’s confusing to me, as a developer is the inconsistent way Twitter handles line breaks. For example…
Twitter’s website ignores line breaks.
Twitter Mobile, however, does line breaking correctly.
Twitter has no style guide for rendering and – if it did – appears to render inconsistently. So, what’s the correct thing to do?
In my opinion, respect the user. If a user has indicted that they want something, it is a developer’s role to implement it (unless it causes severe problems for the system).
How to achieve it?
You could use all sorts of complex regular expressions to get line breaks and convert them.
Thankfully, PHP has this function built in with nl2br() which will give you (X)HTML line breaks wherever a break occurs in the text.
*Technically, Unicode Art. But that doesn’t have the same ring to it.