how to find string length in laravel

In this section we will see how to find string length in laravel . You can use laravel helper method Str::length() to find string length or you can use php strlen() method .

Let's take a look at the following example to understand how it actually works.


Example 1

Str::length method in laravel

<?php

namespace App\Http\Controllers;

use App\Models\Blog;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class BlogController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $stringLength = Str::length('laravel best framework');

        dd($stringLength);
    }
}


Example 2

let see another example

<?php

namespace App\Http\Controllers;

use App\Models\Blog;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class BlogController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $blog = Blog::find(1);
        $blogLength = Str::length($blog->description);

        dd($blogLength);
    }
}


You can also find string length by using strlen() method. it is php in build method

PHP strlen() method

<?php

namespace App\Http\Controllers;

use App\Models\Blog;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class BlogController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $stringLength = 'laravel best framework';

        dd(strlen($stringLength));
    }
}