Jasny's PHP functions
, (*1)
A set PHP functions that should have been part of PHP's core libraries., (*2)
Example, (*3)
$found = str_contains($string, 'foo') && array_contains($array, ['all', 'of', 'these']);
// VS
$found = strpos($string, 'foo') !== false && count(array_intersect($array, ['all', 'of', 'these'])) === 3;
But wait, there's more...
If you like these functions, you'll love the Improved PHP library. Go and check it out., (*4)
Installation
composer require jasny\php-functions
Usage
All functions are in the Jasny
namespace., (*5)
use function Jasny\str_contains; // Import functions
str_contains('moonrise', 'on');
Jasny\slug('Foo bár'); // or use directly
To import all the functions to the global namespace require 'global.php' anywhere in your application., (*6)
require_once 'vendor/jasny/php-functions/global.php';
Alternatively, add it to the autoload
section of composer.json
., (*7)
"autoload": {
"files": [
"vendor/jasny/php-functions/global.php"
]
}
Type functions
is_associative_array
boolean is_associative_array(mixed $var)
Check if variable is an associative array., (*8)
is_numeric_array
boolean is_numeric_array(mixed $var)
Check if variable is a numeric array., (*9)
is_stringable
boolean is_stringable(mixed $var)
Check if variable can be cast to a string. Returns true for all scalar values except booleans and objects that have a
__toString
method., (*10)
objectify
stdClass|mixed objectify(array|mixed $var)
Turn an associated array into a stdClass
object recursively., (*11)
arrayify
array|mixed arrayify(stdClass|mixed $var)
Turn an stdClass
object into an associated array recursively., (*12)
get_type_description
string get_type_description(mixed $var)
Get the type of a variable in a descriptive way. E.g. "stream resource" and "DateTime object"., (*13)
expect_type
expect_type(mixed $var, string|string[] $type, string $throwable = null, string $message = null)
Validate that an argument has a specific type., (*14)
As type you can specify any internal type, including callable
and object
, a class name or a resource type (eg
stream resource
). Typed arrays are not supported., (*15)
By default a TypeError
(PHP 7) is thrown. You can specify a class name for any Throwable
class. For PHP 5 you must
specify the class name., (*16)
The message may contain a %s
, which is replaced by the type of $var
., (*17)
Example
expect_type($input, ['array', 'stdClass']);
expect_type($output, ['array', 'stdClass'], 'UnexpectedValueException', "Output should be an array or stdClass object, got a %s");
Array functions
array_only
array array_only(array $array, array $keys)
Return an array with only the specified keys., (*18)
array_without
array array_without(array $array, array $keys)
Return an array without the specified keys., (*19)
array_contains_all
boolean array_contains_all(array $array, array $subset, boolean $strict = false)
Check if an array contains all values in a set., (*20)
This function works as expected with nested arrays or an array with objects., (*21)
array_contains_all_assoc
boolean array_contains_all_assoc(array $array, array $subset, boolean $strict = false)
Check if an array contains all values in a set with index check., (*22)
This function works as expected with nested arrays or an array with objects., (*23)
array_contains_any
boolean array_contains_any(array $array, array $subset, boolean $strict = false)
Check if an array contains any value in a set., (*24)
This function works as expected with nested arrays or an array with objects., (*25)
array_contains_any_assoc
boolean array_contains_any_assoc(array $array, array $subset, boolean $strict = false)
Check if an array contains any value in a set with index check., (*26)
This function works as expected with nested arrays or an array with objects., (*27)
array_find
mixed array_find(array $array, callable $callback, int $flag = 0)
Find an element of an array using a callback function. Returns the value or FALSE if no element was found., (*28)
Flag determining what arguments are sent to callback:, (*29)
-
ARRAY_FILTER_USE_KEY
- pass key as the only argument to callback instead of the value
-
ARRAY_FILTER_USE_BOTH
- pass both value and key as arguments to callback instead of the value
- Default is
0
which will pass value as the only argument to callback instead.
array_find_key
string|int|false array_find_key(array $array, callable $callback, int $flag = 0)
Find a key of an array using a callback function. Returns the key or FALSE if no element was found., (*30)
array_flatten
array function array_flatten(string $glue, array $array)
Flatten a nested associative array, concatenating the keys., (*31)
Example
$values = array_flatten('.', [
'animal' => [
'mammel' => [
'ape',
'bear'
],
'reptile' => 'chameleon'
],
'colors' => [
'red' => 60,
'green' => 100,
'blue' => 0
]
]);
Will become, (*32)
[
'animal.mammel' => [
'ape',
'bear'
],
'animal.reptile' => 'chameleon',
'colors.red' => 60,
'colors.green' => 100,
'colors.blue' => 0
]
array_join_pretty
string array_join_pretty(string $glue, string $and, array $array);
Join an array, using the 'and' parameter as glue the last two items., (*33)
Example
echo "A task to " . array_join_pretty(", ", " and ", $chores) . " has been created.", PHP_EOL;
echo array_join_pretty(", ", " or ", $names) . " may pick up this task.", PHP_EOL;
String functions
str_starts_with
boolean str_starts_with(string $string, $string $substr)
Check if a string starts with a substring., (*34)
str_ends_with
boolean str_ends_with(string $string, string $substr)
Check if a string ends with a substring., (*35)
str_contains
boolean str_contains(string $string, string $substr)
Check if a string contains a substring., (*36)
str_before
string str_before(string $string, string $substr)
Get a string before the first occurence of the substring. If the substring is not found, the whole string is returned., (*37)
str_after
string str_after(string $string, string $substr)
Get a string after the first occurence of the substring. If the substring is not found, an empty string is returned., (*38)
str_remove_accents
string str_remove_accents(string $string)
Replace characters with accents with normal characters., (*39)
str_slug
string str_slug(string $string, string $glue = '-')
Generate a URL friendly slug from the given string., (*40)
Cast functions
camelcase
string camelcase(string $string)
Turn a sentence, StudlyCase, snake_case or kabab-case into camelCase., (*41)
studlycase
string studlycase(string $string, $ucfirst = true)
Turn a sentence, camelCase, snake_case or kabab-case into StudlyCase., (*42)
snakecase
string snakecase(string $string)
Turn a sentence, StudlyCase, camelCase or kabab-case into snake_case., (*43)
kababcase
string kababcase(string $string)
Turn a sentence, StudlyCase, camelCase or snake_case into kabab-case., (*44)
uncase
string uncase(string $string)
Turn StudlyCase, camelCase, snake_case or kabab-case into a sentence., (*45)
Server functions
ip_in_cidr
boolean ip_in_cidr(string $ip, string $cidr)
Check if an IP address is in a CIDR block., (*46)
Works with IPv4 and IPv6., (*47)
ipv4_in_cidr
boolean ipv4_in_cidr(string $ip, string $cidr)
Check if an IPv4 address is in a CIDR block., (*48)
ipv6_in_cidr
boolean ipv6_in_cidr(string $ip, string $cidr)
Check if an IPv6 address is in a CIDR block., (*49)
inet_to_bits
string inet_to_bits(string $inet)
Converts inet_pton output to string with bits., (*50)
File functions
file_contains
boolean file_contains(string $filename, string $string)
Check if a string is present in the contents of a file., (*51)
This function is memory usage friendly by not loading the whole contents of the file at once., (*52)
fnmatch_extended
boolean fnmatch_extended(string $pattern, string $path)
Match path against wildcard pattern. This is an extended version of fnmatch., (*53)
-
?
Matches a single character, except /
-
#
Matches any decimal characters (0-9)
-
*
Matches any characters, except /
-
**
Matches any characters
-
[abc]
Matches a
, b
or c
-
{ab,cd,ef}
Matches ab
, cd
or ef
Function handling functions
call_user_func_assoc
mixed call_user_func_assoc(callable $callback, array $param_arr)
Call a callback with named parameters as associative array., (*54)
Object functions
object_get_properties
array object_get_properties(object $object, bool $dynamic = true)
Get the public properties of an object., (*55)
Unlike get_object_vars
, this method will return only public properties regardless of the scope., (*56)
The dynamic
flag controls if the output should be filtered, so only properties defined in the class are set., (*57)
object_set_properties
array object_get_properties(object $object, array $data, bool $dynamic = true)
Set the public properties of an object., (*58)
The dynamic
flag controls if $data
should be filtered, so only properties defined in the class are set., (*59)