PHP code example of snicco / str-arr

1. Go to this page and download the library: Download snicco/str-arr library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

snicco / str-arr example snippets


use Snicco\Component\StrArr\Str;

$subject = 'snicco.io';

Str::contains($subject, '.io') // true
Str::containsAll($subject, ['.io', '.com']) // false
Str::containsAny($subject, ['.io', '.com']) // true

Str::studly('snicco str-arr'); // Snicco StrArr

Str::ucfirst($subject); // Snicco.io

Str::startsWith($subject, 'snicco') // true

Str::endsWith($subject, '.io') // true
Str::doesNotEndWith($subject, '.io') // false

Str::afterFirst($subject, 'c') // co.io
Str::afterLast($subject, 'c') // o.io

Str::betweenFirst($subject, 'c', 'o') // o
Str::betweenLast($subject, 'c', 'o') // co.io

Str::beforeFirst($subject, 'o') // snicc
Str::beforeLast($subject, 'o') // snicco.i

Str::substr($subject, -3) // .io

// This accepts any regex pattern. * will be replaced with ".*"
Str::is($subject, 'snicco.*') // true

Str::replaceFirst($subject, 'c', 'k') // snikco.io
Str::replaceAll($subject, 'c', 'k') // snikko.io

Str::pregReplace($subject, 'c', '/\.\w{2}/', '.de') // snicco.de

use Snicco\Component\StrArr\Arr;use Snicco\Component\StrArr\Str;

$array = [
    'foo' => 'bar'
    'baz' => 'biz'
    'boom' => [
        'bang' => 'pow'
    ]   
]

Arr::only($array, ['foo']) // ['foo'=>'bar']

// Returns the first array element
Arr::first($array) // bar

// Returns the first element matching the condition
Arr::first(
    $array, 
    fn(string $value, string $key) => Str::startsWith($key, 'f')
); // bar

// With a default value
Arr::first($array, fn($value) => is_int($value), 'default') // default

Arr::random($array, 1) // returns one random value.

Arr::toArray('foo') // ['foo']
Arr::toArray([]) // []
Arr::toArray(['foo']) // ['foo']

// Checks if all keys are strings
Arr::isAssoc($array) // true

Arr::isList($array) // false
Arr::isList(['foo', 'bar']) // true

Arr::get($array, 'foo') // bar
Arr::get($array, 'bogus') // null
Arr::get($array, 'bogus', 'default') // default
Arr::get($array, 'boom.bang') // pow

// passed by reference here
Arr::set($array, 'boom.bang', 'POW');

Arr::has($array, 'foo') // true
Arr::has($array, 'bogus') // false
Arr::has($array, 'boom.bang') // true

Arr::hasAll(['foo', 'bogus']) // false

Arr::hasAny(['foo', 'bogus']) // true

// Checks if the passed value is either array or ArrayAccess
Arr::accessible($array) // true

Arr::mergeRecursive($array, ['boom' => ['bang' => 'POW', 'new' => 'NEW']]) 
// [ 'foo' => 'bar'
//    'baz' => 'biz'
//    'boom' => [
//        'bang' => 'POW',
//        'new => 'NEW'
//    ]
// ]

Arr::keyExists($array, 'foo') // true

Arr::flatten($array) // ['bar', 'biz', 'pow']

Arr::except($array, ['foo', 'baz'])
// [
//    'boom' => [
//        'bang' => 'POW',
//        'new => 'NEW'
//    ]
// ]

// Passed by reference here
Arr::remove($array, 'boom.bang');
// [ 'foo' => 'bar'
//    'baz' => 'biz'
//    'boom' => []
// ]