From the course: PHP Tips, Tricks, and Techniques

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Converting new lines to real paragraphs

Converting new lines to real paragraphs

From the course: PHP Tips, Tricks, and Techniques

Converting new lines to real paragraphs

- [Instructor] Hi, I'm David Powers, and welcome to this week's edition of PHP Tips, Tricks, and Techniques, designed to help you become a smarter, more productive PHP developer. This week's tip is fairly simple, but nevertheless useful. I'll also tell you how you can help shape future editions of this series. But first, let's dive into some PHP code. This script gets the contents of a text file and parses it to the nl2br function. Then in the body of the HTML file, it outputs the text between a pair of paragraph tags. Using nl2br like this is probably one of the first techniques you learned in PHP. When the page is loaded into a browser, this series of paragraphs looks fine. But if I right-click and view page source, we can see that what's been created is only the illusion of paragraphs. Nl2br simply inserts HTML br text wherever there's new line character. And that's where the function gets its name from. New line to br tag. What's the problem, you might ask. This looks fine. Well…

Contents