A set of useful PHP functions.
All functions are in the Jasny
namespace.
use function Jasny\str_contains; // Import functions
str_contains('moonrise', 'on');
Jasny\slug('Foo bár'); // or use directly
composer require jasny\php-functions
array_unset(array &$array, string $key)
Walk through the array and unset an item with the key. Clones object, so the original aren't modified.
array array_only(array $array, string $key)
Return an array with only the specified keys.
array array_without(array $array, string $key)
Return an array without the specified keys.
array extract_keys(array $array, array $keys)
Get items from array identified by the keys. Will not trigger notices if a key doesn't exist.
$keys
may be a mix of a index an assosiated array. With an indexed item, the value is used as key of $array
. For an
associated item, the key is use as key of $array
and the value is used as default. The default value is picked if
$array
doesn't has the key or the value is null
(using isset()
).
list($foo, $bar, $useAll) = extract_keys($_GET, ['foo', 'bar', 'all' => false]);
boolean str_starts_with(string $string, $string $substr)
Check if a string starts with a substring.
boolean str_ends_with(string $string, string $substr)
Check if a string ends with a substring.
boolean str_contains(string $string, string $substr)
Check if a string contains a substring.
string str_remove_accents(string $string)
Replace characters with accents with normal characters.
string str_slug(string $string, string $glue = '-')
Generate a URL friendly slug from the given string.
string camelcase(string $string)
Turn a sentence, StudlyCase, snake_case or kabab-case into camelCase.
string studlycase(string $string, $ucfirst = true)
Turn a sentence, camelCase, snake_case or kabab-case into StudlyCase.
string snakecase(string $string)
Turn a sentence, StudlyCase, camelCase or kabab-case into snake_case.
string kababcase(string $string)
Turn a sentence, StudlyCase, camelCase or snake_case into kabab-case.
string uncase(string $string)
Turn StudlyCase, camelCase, snake_case or kabab-case into a sentence.
boolean ip_in_cidr(string $ip, string $cidr)
Check if an IP is in a CIDR block.
boolean file_contains(string $filename, string $string)
Check if a string is present in the contents of a file.
This function is memory usage friendly by not loading the whole contents of the file at once.
fnmatch(string $pattern, string $path)
Match path against wildcard pattern. This is an extended version of fnmatch.
?
Matches a single character, except/
#
Matches any decimal characters (0-9)*
Matches any characters, except/
**
Matches any characters[abc]
Matchesa
,b
orc
{a,b,c}
Matchesa
,b
orc
Only access private / protected properties and methods for testing and debugging.
mixed get_private_property(object $object, string $property);
Get the value of a private or protected property
mixed call_private_method(object $object, string $method, ...);
Call a private or protected method
mixed call_private_method(object $object, string $method, array $args);
Call a private or protected method, giving the arguments as array