Anna Jeanine
Anna Jeanine

Reputation: 4125

Get a substring in Laravel PHP

I am currently trying to implement and edit form in my application where the default values are the ones from the item which need to be adjusted. My database encrypts some values and adjusted them before storing. What I need is to get the substring of 5 digits between the :// and . characters of the decrypted value. My form is formatted as:

{!! Form::hidden('id', Lang::get('token.id')); !!}
{!! Form::text('id', Crypt::decrypt($token->url), ['class' => 'form-control', 'maxlength' => 5, 'placeholder' => Lang::get('token.id_placeholder')]); !!}

Could someone help me get the substring of 5 digits between :// and . from the decrypted value to use as a default value in my form?

Upvotes: 1

Views: 19560

Answers (2)

Wesley Gonçalves
Wesley Gonçalves

Reputation: 2305

As of Laravel 6+, you can use the startsWith method.

use Illuminate\Support\Str;

$result = Str::startsWith('This is my name', 'This');
// true

As of Laravel 8+, you can pass multiple values to be tested against the string.

If an array of possible values is passed, the startsWith method will return true if the string begins with any of the given values:

use Illuminate\Support\Str;

$result = Str::startsWith('This is my name', ['This', 'That', 'There']);
// true

Upvotes: 2

Lucas Franco
Lucas Franco

Reputation: 382

you can do it this way:

<?php
$a = 'https://12345.test.com';
$digits = substr($a, strpos($a, '://') + 3, 5);

Hope it works.

Edit: was missing part of the code

Upvotes: 4

Related Questions