SoulAiker
SoulAiker

Reputation: 139

Why does the sub string not working? Is there way to sub string the result?

I have current module where I need to list all zip files from S3 AWS to the html tables. now I want to sub-string the 2 digit and the last digit of the number. however when i try to var dump the result still same number the substr not working can you help me guys to find out how to solved this?.

Example.

Number: 1150

Result must be: 11 and 50

I will show you guys my sample code that I already created.

    $storage = Storage::disk('s3');
    $client = $storage->getAdapter()->getClient();
    $command = $client->getCommand('ListObjects');
    $command['Bucket'] = $storage->getAdapter()->getBucket();
    $command['Prefix'] = '11-10-2019';
    $result = $client->execute($command);



    foreach ($result['Contents'] as $file) {

        $base_name = basename($file['Key']);
        $trim_1 = str_replace('exp', '', $base_name);
        $trim_2 = substr($trim_1, 1, -4);
        var_dump($trim_2);  



    }

My Output is this:

enter image description here

Upvotes: 1

Views: 50

Answers (1)

pr1nc3
pr1nc3

Reputation: 8348

$number = 1150;

$trim1 =  substr($number, 0, 2);
$trim2 = substr($number,-2);

echo $trim1;
echo $trim2;

Based on what you ask you can achieve this by doing something like that.

The output is 11 & 50

Since you are using laravel you can use laravel helper for that which is pretty much the same and go for something like:

Str::substr($number, 2);
Str::substr($number, -2);

Don't forget to include the helper at the top of your file:

use Illuminate\Support\Str;

Upvotes: 2

Related Questions