Quick Dev Tip: Using nl2br(e()) to Format Text in Laravel with PHP

Andrea Gernone 🫀🥷
tech bits pub
Published in
4 min readMar 9, 2023

--

Do you want to know how to format text in Laravel with PHP? There is no need to look any further! In this tutorial, we will look at how to format text in Laravel using the nl2br(e()) function. This function can be a useful tool in your development arsenal, allowing you to format text in your web apps quickly and easily.

Photo by Andrew Neel on Unsplash

Introduction

Any web program must include text formatting. It can increase the readability of your content, the user experience, and even search engine optimization.

The nl2br(e()) function in Laravel can be used to format text.

This function

htmlspecialchars(nl2br($text), ENT_QUOTES, 'UTF-8', false)

converts newline characters in a string to HTML line breaks and escapes special characters.

It accepts a string and returns one with newline characters changed to HTML line breaks and special characters escaped.

Photo by Sigmund on Unsplash

There are four inputs to the nl2br(e()) function:

  • $string: The formatted input string.
  • $doubleEncode: A Boolean value that specifies whether special characters should be double-encoded. The value false is the default.
  • $charset: The encoding character set to use. ‘UTF-8’ is the default value.
  • $nl2br: A Boolean value indicating whether newline characters should be converted to HTML line breaks. True is the default value.

Making Use of the nl2br(e()) Function

Simply give the string you want to format as the first parameter to the nl2br(e()) function.

Assume you have a string that includes newline characters and special characters that must be escaped. For example you’re building a blog or a platform with text input and you just want to use a simple textarea and then just print the text as the user wrote it.

In that instance, the nl2br(e()) function can be used to format the string as follows:

$string = "This is a string\nwith newline characters and <special> characters.";
$formattedString = nl2br(e($string));
echo $formattedString;

This will output the following HTML:

This is a string<br>
with newline characters and &lt;special&gt; characters.

Formatting User Input

Formatting user input is a common use case for the nl2br(e()) function.

User input can contain special characters that need to be escaped and newline characters that need to be converted to HTML line breaks.

To format user input, simply pass the input string to the nl2br(e()) function as follows:

To format user input, simply pass the input string to the nl2br(e()) function as follows:

Or in Laravel in your .blade.php file:

<p><span class="fw-bold">Post body:</span> {!! nl2br(e($post->text)) !!}</p>

Conclusion

Conclusion

Photo by Masaaki Komori on Unsplash
Photo by bruce mars on Unsplash

FAQs

FAQs

  1. What is the purpose of the nl2br(e()) function?
    The nl2br(e()) function is used in Laravel with PHP to format text.
  2. What is the purpose of the nl2br(e()) function?
    The nl2br(e()) function escapes special characters and transforms newline characters to HTML line breaks.
  3. Is it possible to use the nl2br(e()) function to format user input?
    Yes, you can format user data with the nl2br(e()) function.
  4. Is there anything to worry about when using the nl2br(e()) function?
    To prevent cross-site scripting (XSS) attacks, it is usually considered a good practice to escape special characters in user input. The nl2br(e()) function can assist by escaping special characters, but it is still necessary to validate and sanitize user input before using it in your program.
  5. Is it possible to modify the nl2br(e()) function?
    Yes, the nl2br(e()) function can be customized by providing various parameter values. You can, for example, alter the character set or turn off the conversion of newline characters to HTML line breaks.
  6. Is the nl2br(e()) function only available in Laravel?
    The nl2br(e()) function is a PHP function, not a Laravel function.
  7. Is it possible to use the nl2br(e()) function to format HTML?
    No, the nl2br(e()) function is intended to format plain text rather than HTML. You should use a specialized HTML formatting library or function if you need to format HTML.
  8. Is it possible to use the nl2br(e()) function in conjunction with other text formatting functions?
    Yes, you can combine the nl2br(e()) function with other text formatting functions to accomplish more complex formatting. You can use it with the Markdown parser, for example, to format Markdown text with HTML line breaks.
  9. What are some of the most frequent applications for the nl2br(e()) function?
    The nl2br(e()) function is commonly used to format user input, format plain text content in web applications, and show formatted text in email messages.

Finally, the nl2br(e()) function in Laravel with PHP is a helpful tool for formatting text. It can help make your material more readable and improve your users’ user experience. With just a few lines of code, you can quickly format text in your web applications using the nl2br(e()) function. To avoid security vulnerabilities in your application, always validate and sanitize user input.

Hope you solved your problem!

Cheers 🍻

Andrea

If you liked this article, consider buying me a coffee! 🚀 ⭐️ 😎

Buy me a Coffee! ☕️

--

--

Andrea Gernone 🫀🥷
tech bits pub

I am a full stack professional software developer at @apuliasoft. Based in Italy and passionate about anything that enriches my soul. Also I love to eat