nl2br() line break in PHP with example

nl2br() line break in PHP with example

The nl2br() line break in PHP is an inbuilt function provided by PHP. It breaks the line of paragraph elements in HTML if the paragraph contains newlines. In many cases, we see that if we insert a small paragraph or large paragraph in the MySQL database then it is inserted as a whole string.

But when we echo the value of the paragraph from the MySQL database using PHP then we can not see the paragraph exactly in the way it has been inserted line by line. In order to achieve this, the nl2br() function is used to fetch the value of the paragraph as same as it was inserted. So, I am going to explain the example in a step-by-step process.

Also read, PHP difference between two dates in years, months and days

Explanation of nl2br() Line break in PHP

Step 1:- Create the database in Mysql.


Step 2:- Create a table in the database as shown below.


DDL information of the table

CREATE TABLE line_break (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
message varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
created_at timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
updated_at timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

Step 3:- Create a file with a .php extension in the root directory of your local server. In my case, the filename is paragraph.php.


Step 4:- Add the below code to paragraph.php as shown below

paragraph.php

<?php

//Connect the database

$servername='localhost';
$username="root";
$password="";
try
{
  $con=new PDO("mysql:host=$servername;dbname=test_db",$username,$password);
  $con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
  //echo 'connected';
}
catch(PDOException $e)
{
  echo '<br>'.$e->getMessage();
}

//Get the data from the user

if(isset($_POST['submit']))
{
  $message = $_POST['message'];

//Insert the data in mysql database table

$sql="INSERT INTO line_break(message) VALUES('$message')";
      $stmt=$con->prepare($sql);
      $stmt->execute();

  }

?>

<html>
<head>
<title>Line Break in PHP</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<style>
.container{
    width:40%;
    height:30%;
    padding:20px;
}
</style>

</head>
<body>
   <div class="container">
    <h3 align="center"><u>nl2br() line break in PHP</u></h3>
    <br/><br/>
    <form class="form-horizontal" action="#" method="post">
              <div class="form-group">
                <label class="control-label col-sm-2" for="email">Message:</label>
                    <div class="col-sm-10">
                      <textarea name="message" rows="5" class="form-control"></textarea>
                    </div>
               </div>
              <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                  <button type="submit" class="btn btn-primary btn-sm" name="submit">Save</button>
                </div>
              </div>
    </form>
    <br>
    <h4>Output:-</h4>
    <p>

      <?php
//fetch data from the table

      $stmt1=$con->query("select * from line_break order by id asc");
      $stmt1->execute();
      $rows=$stmt1->fetchAll(PDO::FETCH_ASSOC);
      if(!$rows){
        echo 'No data found';
      }
      else{
        foreach($rows as $value){
          echo nl2br($value['message']);
        }
      }

      ?>
     
    </p>

</div>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</body>
</html>


Step 5:- Now, add the below code to insert the value of the paragraph in the table of the database.

<?php
//Insert the data in mysql database table

$sql="INSERT INTO line_break(message) VALUES('$message')";
      $stmt=$con->prepare($sql);
      $stmt->execute();

  }
?>


Step 6:- Open the browser and paste the below URL.

http://localhost/paragraph.php


Step 7:- Now enter the text in the paragraph and submit.

When I entered the text as shown below

nl2br() line break in PHP with example

Output:-

Bootstrap is a free CSS framework. Bootstrap 3 is the most stable version of Bootstrap

When I entered the text as shown below

nl2br() line break in PHP with example

Output:-

Bootstrap 4 is a newer version of Bootstrap;
with new components, a faster stylesheet,
and more responsiveness.

Conclusion:- I hope this tutorial will help you to understand the concept and use of the nl2br function. If there is any doubt then please leave a comment below

Leave a Comment

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

KUPU178

KUPU178

KUPU178

hoki311

hoki311

hoki311

hoki311

hoki311

RATU311

KUPU178

HOKI311

THOR311

THOR311

login hoki311

daftar hoki311

hoki 311

link hoki311

hoki311

RATU311

hoki311

hoki311

hoki311

hoki311

KUPU178

thor311

thor311

thor311

ceri188

ceri188

KUPU178

ceri188

ceri188

link ceri188

KUPU178

KUPU178

KUPU178

kupu178

ceri188

ceri188

ceri188

ceri188

ceri188

THOR311

ceri188

games online ceri188

link login ceri188

ceri188

thor311

RATU311

RATU311

RATU311

sbobet88

sbobet

parlay

mix parlay

judi bola online

thor311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

THOR311

THOR311

THOR311

thor311 RTP

thor311

slot gacor

kupu178

slot mahjong

ceri188

sabung ayam

ratu311

THOR311

THOR311

THOR311

thor311 aplikasi

ratu311 sabung ayam

slot gacor

RATU311

ceri188

ceri188

ceri188

ceri188

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

ceri188

ceri188

ceri188 alternatif

ceri188

link ceri188

ceri188

RATU311

THOR311

THOR311

THOR311

RATU311

RATU311

RATU311

Togel Online

KUPU178

Situs Togel

KUPU178

ceri188

ceri188

KUPU178

KUPU178

ceri188

ceri188

ratu311

ceri188

ceri188

ratu311

KUPU178

THOR311

ceri188

ceri188

THOR311

THOR311

RATU311

thor311 tajen bali

thor311 alternatif

situs toto4d

thor311 toto4d

thor311 akses

ceri188

daftar ceri188

cery188

ceri188

slot gacor

kupu 178

kupu178

ceri1888

ceri188

slot online

hoki311

togel online

ceri188

ding dong

ratu311

slot gacor

kupu178

live casino

kupu178

mix parlay

ceri188

judi bola online

thor311 slot

THOR311

RATU311

KUPU178

THOR311

ceri188

ratu311

slot online

hoki311

kupu178

togel online

kupu178

slot gacor

ceri188

judi bola online

ceri188

ratu311

kupu178

RATU311

RATU311

live casino roullete

ceri188

slot pg soft

kupu178

KUPU178

THOR311

RATU311

KUPU178

THOR311

THOR311

thor311 domino qq

thor311 akses

thor dingdong

ceri188

ceri188

kupu178

kupu178

togel

kupu178

mix parlay

ceri188

slot online

kupu178

THOR311

THOR311

KUPU178

kupu178

hoki311

togel online

kupu178

ceri188

ceri188

www.vrcorporate.in

phbalance.vn

togel hongkong

kupu178

slot gacor

kupu178

kupu178

ceri188

kupu178

KUPU178

RATU311

slot online

kupu178

THOR311

KUPU178

slot mahjong ways

hoki311

slot gacor

ceri188

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

toto macau

KUPU178

Slot Gacor

KUPU178

RATU311

Mahjong ways

KUPU178

KUPU178

KUPU178

RATU311

RATU311

Toto Togel

KUPU178

CERI188

Slot Sweet Bonanza 2500

KUPU178

RATU311

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

THOR311

HOKI311

KUPU178

Slot Mahjong Ways

KUPU178

CERI188

Slot Online Resmi

KUPU178

RATU311 SLOT ONLINE

KUPU178

KUPU178: Link Akses Resmi

KUPU178: Strategi Menang Di Game Live Casino

KUPU178

KUPU178: Minimal 200 Rupiah

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

CERI188

RATU311: Alternatif karnpuracollege

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

RATU311 NONTON BOLA

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

RATU311 judi bola

KUPU178

HOKI311

RATU311

THOR311

HOKI311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

KUPU178

RATU311

THOR311