How to insert a line break in PHP string ? Last Updated : 07 Aug, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we will discuss how to insert a line break in a PHP string. We will get it by using the nl2br() function. This function is used to give a new line break wherever '\n' is placed.Syntax:nl2br("string \n");where the string is the input string.Example 1: PHP Program to insert a line break in a string. PHP <?php // Insert a new line break echo nl2br("This is first line \n This is second line"); ?> OutputThis is first line <br /> This is second lineExample 2: PHP <?php // Insert a line break with new line echo nl2br("This is first line \n This is second " + "line \n This is third line \n This is fourth line"); ?> OutputThis is first line <br /> This is second line <br /> This is third line <br /> This is fourth lineUsing Nowdoc SyntaxUsing Nowdoc syntax in PHP, you can insert a line break by defining a multi-line string. This syntax treats everything literally, without parsing variables or special characters.Example: PHP <?php $string = <<<'EOD' Hello, World! EOD; echo $string . PHP_EOL; // Outputs to the console ?> OutputHello, World! Using PHP_EOL ConstantThe PHP_EOL constant in PHP represents the end-of-line character appropriate for the platform PHP is running on (e.g., "\n" on Unix-like systems, "\r\n" on Windows). You can use this constant to insert platform-independent line breaks in a string.Example: PHP <?php function insertLineBreakUsingEOL($string) { return str_replace("\n", PHP_EOL, $string); } $inputString = "Hello,\nWorld!"; $formattedString = insertLineBreakUsingEOL($inputString); echo nl2br($formattedString); ?> OutputHello,<br /> World!Using str_replace()The str_replace() function in PHP can replace all occurrences of a substring within a string. You can use this function to replace the newline character \n with <br /> tags for HTML output.Example: PHP <?php function insertLineBreakUsingStrReplace($string) { return str_replace("\n", "<br />", $string); } $inputString = "This is the first line \nThis is the second line"; $formattedString = insertLineBreakUsingStrReplace($inputString); echo $formattedString; ?> OutputThis is the first line <br />This is the second line Comment More infoAdvertise with us Next Article How to insert a line break in PHP string ? gottumukkalabobby Follow Improve Article Tags : Web Technologies PHP PHP-string PHP-function PHP-Questions +1 More Similar Reads How to insert a line break in a String PL/SQL In PL/SQL, inserting line breaks into VARCHAR or NVARCHAR strings can be a good way to enhance the readability and presentation of our data. Whether we are formatting output for display or preparing text for storage, knowing how to insert line breaks is a helpful skill. Here, we will explore techniq 5 min read How to Insert a Line Break in a String PostgreSQL ? In PostgreSQL, managing strings efficiently is crucial, especially when dealing with VARCHAR and NVARCHAR data types. One common challenge developers face is inserting line breaks within these strings. In this article, we'll explore different approaches to tackle this issue, allowing us to format ou 5 min read How to Insert a Line Break VARCHAR String in MySQL When dealing with textual data in MySQL databases, maintaining proper formatting is important for readability and clarity. One common formatting requirement is to insert line breaks within VARCHAR and NVARCHAR strings. In this article, we will understand How to insert a line break in a MySQL VARCHAR 3 min read How to Insert Line Break in SQL Server String? In SQL Server there are various datatypes like int, float, char, nchar, etc but especially while we are dealing with text in VARCHAR and NVARCHAR columns, we might run into situations where we need to make the text look cleaner by adding line breaks. This could be for better organization, and readab 4 min read How to Insert a Line Break in a SQLite VARCHAR String SQLite is a popular relational database management system known for its simplicity and flexibility. However, inserting a line break in a VARCHAR/NVARCHAR string can be challenging due to SQLite's limited support for special characters. In this article, we will explore different approaches to inserti 4 min read How to Insert a Line Break in a SQL VARCHAR In SQL, VARCHAR and NVARCHAR are widely used data types for storing character data. It may sometimes be necessary to insert line breaks into these string values ââfor better readability or to display the information in a formatted manner. Although SQL itself does not have a specific newline characte 4 min read How to Insert New Line in R Shiny String Inserting a new line in a string within an R Shiny application is a common task, especially when dealing with text outputs in UI components such as text Output, verbatimTextOutput, or even within HTML tags. Understanding how to insert new lines correctly helps improve the readability and organizatio 4 min read How to create a New Line in PHP ? When working with text output in PHP, like creating a webpage, sending an email, or saving to a file, it's important to know how to add a new line. Adding a new line helps break up the text, makes it easier to read, and keeps it looking right whether it's shown in a browser, a terminal, or a file.Ho 2 min read How to set a single line break in HTML5 ? In this article, we will add a single line break using the <br> tag. It s used to give the single line break. It is the empty tag so it does not contain end tag. Syntax: <br> Example 1: This example uses <br> tag to break the line. html <!DOCTYPE html> <html> <head 1 min read How to limit string length in PHP ? A string is a sequence of characters in PHP. The length of the string can be limited in PHP using various in-built functions, wherein the string character count can be restricted. Table of ContentUsing for loopUsing mb_strimwidth() function Using substr() method Using Regular Expressions with preg_m 6 min read Like