Laravel Check If String Starts with Specific Value Example

By Hardik Savani November 5, 2023 Category : Laravel

Hi Folks,

In this post, we will learn how to check if string starts with specific character in laravel. It's a simple example of laravel string starts with. I would like to share with you laravel str starts with. we will help you to give an example of laravel Str::startsWith example.

The Str::startsWith() method in Laravel is used to check if a string starts with a specified value.

The Str::startsWith() method takes two parameters: the string to search and the value to check if it starts with. It returns true if the string starts with the specified value(s), otherwise it returns false.

let's see the simple example:

Laravel String Starts With in Controller

you can use it with controller like this way:

Controller Code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Str;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index(Request $request)

{

$string = "Hello, Hardik";

$result = Str::startsWith($string, 'Hello');

dd($result);

}

}

Output:

true

Laravel String Starts With in Blade File

you can use it with blade file like this way:

Blade File Code:

<p>{{ Str::startsWith("Hello, Hardik", 'hello') }}</p>

Output:

false

I hope it can help you...

Tags :
Shares