Skip to content

sgpopov/php-helpers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP Helpers

Build Status

String Helpers

Str::contains

Check if a string contains another string/set of strings (case-sensitive).**

$contains = Helpers\Str::contains('This is an example string', 'example');

var_dump($contains); // (bool) true

$contains = Helpers\Str::contains('This is an example string', ['not exists', 'example']);

var_dump($contains); // (bool) true

Str::startsWith

Check if a given string starts with a given substring (case-sensitive).

$startsWith = Helpers\Str::startsWith('This is an example string', 'This is');

var_dump($contains); // (bool) true

$startsWith = Helpers\Str::startsWith('This is an example string', 'this is');

var_dump($contains); // (bool) false

Str::endsWith

Check if a given string ends with a given substring (case-sensitive).

$endsWith = Helpers\Str::endsWith('This is an example string', 'example string');

var_dump($endsWith); // (bool) true

$endsWith = Helpers\Str::endsWith('This is an example string', 'Example string');

var_dump($endsWith); // (bool) false

Str::length

Get string length.

$length = Helpers\Str::length('This is an example string');

var_dump($length); // (int) 25

Str::slug

Returns URL friendly slug version of input string:

  • converts all alpha chars to lowercase
  • converts any char that is not digit, letter or - into - symbols into "-"
  • not allow two "-" chars continued, converte them into only one syngle "-"
$string = Helpers\Str::slug('This iS a sImpLe TEST');

var_dump($string); // (string) 'this-is-a-simple-tests'

Array Helpers

Arr::trim

Trims each array element.

$arr = [
    'key1' => ' value      ',
    'key2' => "value \n",
    'key3' => "value \n\t",
    'key4' => "value \n\t\r",
    'key5' => [
        'key6' => '       Testing started           ',
        'key7' => [
            'key9' => 'Testing started      '
        ],
        'key8' => [
            ' value      ',
            "value \n",
            "value \n\t",
            "value \n\t\r"
        ]
    ]
];

$arr = Helpers\Arr::trim($arr);

var_dump($arr);

/*
(array) [
    'key1' => 'value',
    'key2' => 'value',
    'key3' => 'value',
    'key4' => 'value',
    'key5' => [
        'key6' => 'Testing started',
        'key7' => [
            'key9' => 'Testing started'
        ],
        'key8' => [
            'value',
            'value',
            'value',
            'value'
        ]
    ]
];
*/

Arr::removeEmpty

Removes empty elements from the array.

$arr = [
    1 => [
        'foo' => '',
        'bar' => 1,
        'baz' => 1
    ],
    2 => [
        'foo' => null,
        'bar' => null,
        'baz' => 0
    ],
    3 => [
        'foo' => null,
        'bar' => null,
        'pts_cg' => null
    ],
    'key' => [
        'foo' => '   ',
        'bar' => 1,
        'baz' => 1
    ]
];

$arr = Helpers\Arr::removeEmpty($arr);

var_dump($arr);

/*
(array) [
    1 => [
        'bar' => 1,
        'baz' => 1
    ],
    2 => [
        'baz' => 0
    ],
    'key' => [
        'bar' => 1,
        'baz' => 1
    ]
];
*/

Arr::toArray

Convert a object to an array.

Imporant: if the object has "toArray()" method then the result of it will be returned.

$data = new \stdClass();
$data->foo = 'bar';
$data->bar = new \stdClass();
$data->bar->baz = 'some value';

$arr = Helpers\Arr::toArray($data);

var_dump($arr);

/*
(array) [
    'foo' => 'bar',
    'bar' => [
        'baz' => 'some value',
    ]
];
*/

Arr::flatten

Convert multi-dimensional array into a flat array with hyphen-separated keys.

$arr = [
    'employee' => [
        'id' => 1,
        'position' => [
            'name' => 'PHP Developer',
            'code' => 'php-dev'
        ]
    ],
    'active' => true
];

$separator = '-';

$arr = Helpers\Arr::flatten($data, $separator);

var_dump($arr);

/*
(array) [
    'employee-id' => 1,
    'employee-position-name' => 'PHP Developer',
    'employee-position-code' => 'php-dev',
    'active' => true
];
*/

Arr::isAssoc

Determine if array is associative.

$arr = [1, 2, 3];

$isAssoc = Helpers\Arr::isAssoc($arr);

var_dump($isAssoc); // (bool) false

$arr = [
    'key' => 'value',
    'foo' => [
        'bar' => 'baz'
    ],
    'oi' => 2
];

$isAssoc = Helpers\Arr::isAssoc($arr);

var_dump($isAssoc); // (bool) true

About

Some handy PHP helpers for strings and arrays

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages