Replace \n with br in Laravel

Posted by

Step 1:- Html code

<!DOCTYPE html>
<html>
<head>
    <title>Replace Newlines Example</title>
</head>
<body>
    <h1>Text with Newlines:</h1>
    <pre>{{ $textWithNewlines }}</pre>

    <h1>Text with Line Breaks:</h1>
    <p>{!! $textWithLineBreaks !!}</p>
</body>
</html>

Step 2:- web.php

Route::get('/',[UserController::class,'View'])->name('view');

Step 3:- Controller

 public function View()
    {
        $textWithNewlines = "This is line 1.\nThis is line 2.\nThis is line 3.";
        $textWithLineBreaks = nl2br($textWithNewlines);
    
        return view('importFile', compact('textWithLineBreaks','textWithNewlines'));
    }

Output:

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x