Html string in Laravel aboutthenerd.com

How to Remove HTML Tags from String in Laravel

Hello Geeks. I’ll show you how to remove html tags from a string in Laravel. This is a simple example of removing html tags from a string in Laravel. I’ll show you how to remove html tags from a string in a Laravel controller.

This example works with Laravel 6, Laravel 7, Laravel 8, and Laravel 9.

If we want to remove all HTML tags from the string, we can use PHP’s strip tags() function. I’ll show you a simple example code and output for removing HTML tags from strings in a Laravel application. So, here is the code for the controller and blade files:

app/Http/Controllers/DemoController.php

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $tagsString = "Google <h1>Nice</h1>, Nice to <p>meet</p> you.";
        return view("demo", compact("tagsString"));
    }
}

views/demo.blade.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>aboutthenerd.com</title>
</head>
<body>
    <h1>How to Remove HTML Tags from String in Laravel?-aboutthenerd.com</h1>
    {{ strip_tags($tagsString) }}
</body>
</html>

All the best nerd.

Leave a Reply