A collection of php helper functions
Install via composer
composer require hedii/helpers
- string_without
- string_before
- string_after
- string_between
- string_starts_with
- string_ends_with
- string_length
- string_is
- string_contains
- string_finish
- string_random
- is_url
- class_basename
- is_windows_os
Remove a substring from a string. It returns the original string if the substring is not found.
$string = string_without('This is my name', ' is ');
// Thismy name
$string = string_without('This is my name', 'some string');
// This is my name
Get the string before a delimiter. It returns false if the string does not contains the delimiter.
$string = string_before('This is my name', ' name');
// This is my
$string = string_before('This is my name', 'some string');
// false
Get the string after a delimiter. It returns false if the string does not contains the delimiter.
$string = string_after('This is my name', 'This ');
// is my name
$string = string_after('This is my name', 'some string');
// false
Get the string between two delimiters. It returns false if the string does not contains the two delimiters.
$string = string_between('This is my name', 'This ', ' name');
// is my
$string = string_between('This is my name', 'some', ' string');
// false
Determines if the given string begins with the given value.
$value = string_starts_with('This is my name', 'This');
// true
Determines if the given string ends with the given value.
$value = string_ends_with('This is my name', 'name');
// true
Get the length of the given string.
$length = string_length('abcd');
// 4
Determines if a given string matches a given pattern. Asterisks may be used to indicate wildcards.
$value = string_is('foo*', 'foobar');
// true
$value = string_is('baz*', 'foobar');
// false
Determines if the given string contains the given value.
$value = string_contains('This is my name', 'my');
// true
$value = string_contains('This is my name', ['some string', 'my']);
// true
Adds a single instance of the given value to a string.
$string = string_finish('this/string', '/');
// this/string/
$string = string_finish('this/string/', '/');
// this/string/
Generates a random string of the specified length.
$string = string_random(40);
// 6a2531aabec1fda11b0e0d9eaeb17d7ebfe1cdc5
Determine if a string is a valid url.
is_url('http://example.com');
// true
is_url('tel:+1-111-222-333');
// false
Get the class "basename" of the given object / class.
$basename = class_basename(\Hedii\Helpers\HelpersTest);
// HelpersTest
Determine whether the current environment is Windows based.
is_window_os();
// false
composer test
helpers is released under the MIT Licence. See the bundled LICENSE file for details.
helpers contains some content from Laravel illuminate/support package. See the LARAVEL LICENSE file for details.