I have a textarea field where I allow users to type in plain text or HTML.
I clean the HTML using HTMLPurify, but I want to add a feature to help users who don’t know HTML (and thus use plain-text): convert double line breaks (\n\n, \r\r, \n\r, \r\n) into a <br>. If there are 2 pairs of double line breaks, turn it into 2 <br>s, etc.
Basically:
Hi, this is some text with a SINGLE linebreak after it.
Here's some more text.
This is some text following a double line break.
Will turn into this:
Hi, this is some text with a SINGLE linebreak after it.Here's some more text.
This is some text following a double line break.
Using nl2br() caused problems in that it made using HTML difficult, for example..
<ul>
<li>List item</li>
<li>List item</li>
<li>List item</li>
</ul>
Was turned into
<ul>
<li>List item</li>
<li>List item</li>
<li>List item</li>
</ul>
which caused there to be extra bullet points when there shouldn’t be.
I found a solution a while ago, and now I am answering my one question. HTMLPurify has an option for ‘auto paragraphing’
AutoFormat.AutoParagraph