Posts Tagged ‘autop’

Autop

Monday, June 22nd, 2015 | Programming

PHP has a function called nl2br that converts line breaks into <br> tags. This is really useful if you have content in a database and want to ensure the line breaks are in encoded in HTML without actually having to specify them in the content.

It is a bit of a clumsy tool though. It does not take into account that you might want to use paragraphs, or that you do not want br tags adding inside block elements such as pre or hidden elements such as script and style.

The programmers over at WordPress have done a great job of coming up with their own version that resolves all of these issues and many of us already benefit from it on our sites. Matt Mullenweg has written about it.

However, it is buried in the WordPress codebase, so if you want to use it in a non-Wordpress project, you have to mess about pulling it out of the codebase and inserting it into your own. To save myself from having to do this, I’ve created a project on Github that moves it to a standalone library. You can then drop this into your project using Composer and use away.