🎉 Enjoying this package? Consider sponsoring me on GitHub or buying me a beer.
Usage
All helper functions are automatically available after installation. No configuration required!
Quick Examples
1<?php 2 3require 'vendor/autoload.php'; 4 5// String manipulation 6$slug = str_slug('Hello World'); // "hello-world" 7$plural = str_plural('user', 2); // "users" 8$masked = str_mask('1234567890', '*', 3, 4); // "123****890" 9 10// Array operations11$value = array_get($data, 'user.profile.name', 'Default');12$hasKey = array_has($data, 'user.email');13$first = array_key_first($array);14 15// General utilities16if (blank($value)) {17 // Handle empty value18}19 20$result = transform($value, function ($v) {21 return strtoupper($v);22}, 'default');
Arrays
array_accessible
1/**2 * Determine whether the given value is array accessible.3 *4 * @param mixed $value5 *6 * @return bool7 */8function array_accessible($value): bool
array_add
1/** 2 * Add an element to an array using "dot" notation if it doesn't exist. 3 * 4 * @param array $array 5 * @param string $key 6 * @param mixed $value 7 * 8 * @return array 9 */10function array_add(array $array, string $key, $value): array
array_collapse
1/**2 * Collapse an array of arrays into a single array.3 *4 * @param iterable $array5 *6 * @return array7 */8function array_collapse(iterable $array): array
array_cross_join
1/**2 * Cross join the given arrays, returning all possible permutations.3 *4 * @param iterable ...$arrays5 *6 * @return array7 */8function array_cross_join(...$arrays): array
array_divide
1/**2 * Divide an array into two arrays. One with keys and the other with values.3 *4 * @param array $array5 *6 * @return array7 */8function array_divide(array $array): array
array_dot
1/**2 * Flatten a multi-dimensional associative array with dots.3 *4 * @param iterable $array5 * @param string $prepend6 *7 * @return array8 */9function array_dot(iterable $array, string $prepend = ''): array
array_except
1/**2 * Get all of the given array except for a specified array of keys.3 *4 * @param array $array5 * @param array|string $keys6 *7 * @return array8 */9function array_except(array $array, $keys): array
array_exists
1/**2 * Determine if the given key exists in the provided array.3 *4 * @param \ArrayAccess|array $array5 * @param string|int $key6 *7 * @return bool8 */9function array_exists($array, $key): bool
array_first
1/** 2 * Return the first element in an array passing a given truth test. 3 * 4 * @param iterable $array 5 * @param callable|null $callback 6 * @param mixed $default 7 * 8 * @return mixed 9 */10function array_first(iterable $array, callable $callback = null, $default = null)
array_last
1/** 2 * Return the last element in an array passing a given truth test. 3 * 4 * @param array $array 5 * @param callable|null $callback 6 * @param mixed $default 7 * 8 * @return mixed 9 */10function array_last(array $array, callable $callback = null, $default = null)
array_flatten
1/**2 * Flatten a multi-dimensional array into a single level.3 *4 * @param iterable $array5 * @param int|float $depth6 *7 * @return array8 */9function array_flatten(iterable $array, int|float $depth = INF): array
array_forget
1/**2 * Remove one or many array items from a given array using "dot" notation.3 *4 * @param array $array5 * @param array|string $keys6 *7 * @return void8 */9function array_forget(array &$array, $keys)
array_get
1/** 2 * Get an item from an array using "dot" notation. 3 * 4 * @param \ArrayAccess|array $array 5 * @param string|int|null $key 6 * @param mixed $default 7 * 8 * @return mixed 9 */10function array_get($array, $key, $default = null)
array_has
1/**2 * Check if an item or items exist in an array using "dot" notation.3 *4 * @param \ArrayAccess|array $array5 * @param string|array $keys6 *7 * @return bool8 */9function array_has($array, $keys): bool
array_has_any
1 /**2 * Determine if any of the keys exist in an array using "dot" notation.3 *4 * @param \ArrayAccess|array $array5 * @param string|array $keys6 *7 * @return bool8 */9function array_has_any($array, $keys): bool
array_is_assoc
1/** 2 * Determines if an array is associative. 3 * 4 * An array is "associative" if it doesn't have sequential numerical keys beginning with zero. 5 * 6 * @param array $array 7 * 8 * @return bool 9 */10function array_is_assoc(array $array): bool
array_only
1/**2 * Get a subset of the items from the given array.3 *4 * @param array $array5 * @param array|string $keys6 *7 * @return array8 */9function array_only(array $array, $keys): array
array_pluck
1/** 2 * Pluck an array of values from an array. 3 * 4 * @param iterable $array 5 * @param string|array|int|null $value 6 * @param string|array|null $key 7 * 8 * @return array 9 */10function array_pluck(iterable $array, $value, $key = null): array
array_prepend
1/** 2 * Push an item onto the beginning of an array. 3 * 4 * @param array $array 5 * @param mixed $value 6 * @param mixed $key 7 * 8 * @return array 9 */10function array_prepend(array $array, $value, $key = null): array
array_pull
1/** 2 * Get a value from the array, and remove it. 3 * 4 * @param array $array 5 * @param string $key 6 * @param mixed $default 7 * 8 * @return mixed 9 */10function array_pull(array &$array, string $key, $default = null)
array_query
1/**2 * Convert the array into a query string.3 *4 * @param array $array5 *6 * @return string7 */8function array_query(array $array): string
array_random
1/** 2 * Get one or a specified number of random values from an array. 3 * 4 * @param array $array 5 * @param int|null $number 6 * @param bool $preserveKeys 7 * 8 * @return mixed 9 *10 * @throws \InvalidArgumentException11 */12function array_random(array $array, ?int $number = null, bool $preserveKeys = false)
array_set
1/** 2 * Set an array item to a given value using "dot" notation. 3 * 4 * If no key is given to the method, the entire array will be replaced. 5 * 6 * @param array $array 7 * @param string|null $key 8 * @param mixed $value 9 *10 * @return array11 */12function array_set(array &$array, ?string $key, $value): array
array_shuffle
1/**2 * Shuffle the given array and return the result.3 *4 * @param array $array5 * @param int|null $seed6 *7 * @return array8 */9function array_shuffle(array $array, ?int $seed = null): array
array_sort_recursive
1/** 2 * Recursively sort an array by keys and values. 3 * 4 * @param array $array 5 * @param int $options 6 * @param bool $descending 7 * 8 * @return array 9 */10function array_sort_recursive(array $array, int $options = SORT_REGULAR, bool $descending): array
array_to_css_classes
1/**2 * Conditionally compile classes from an array into a CSS class list.3 *4 * @param array $array5 *6 * @return string7 */8function array_to_css_classes(array $array): string
array_where
1/**2 * Filter the array using the given callback.3 *4 * @param array $array5 * @param callable $callback6 *7 * @return array8 */9function array_where(array $array, callable $callback): array
array_wrap
1/**2 * If the given value is not an array and not null, wrap it in one.3 *4 * @param mixed $value5 *6 * @return array7 */8function array_wrap($value): array
data_fill
1/**2 * Fill in data where it's missing.3 *4 * @param mixed $target5 * @param string|array $key6 * @param mixed $value7 * @return mixed8 */9function data_fill(&$target, $key, $value)
data_get
1/**2 * Get an item from an array or object using "dot" notation.3 *4 * @param mixed $target5 * @param string|array|int|null $key6 * @param mixed $default7 * @return mixed8 */9function data_get($target, $key, $default = null)
data_set
1/** 2 * Set an item on an array or object using dot notation. 3 * 4 * @param mixed $target 5 * @param string|array $key 6 * @param mixed $value 7 * @param bool $overwrite 8 * 9 * @return mixed10 */11function data_set(&$target, $key, $value, bool $overwrite = true)
head
1/**2 * Get the first element of an array. Useful for method chaining.3 *4 * @param array $array5 *6 * @return mixed7 */8function head(array $array)
last
1/**2 * Get the last element from an array.3 *4 * @param array $array5 *6 * @return mixed7 */8function last(array $array)
array_key_first
1/**2 * Get the first key of the given array without affecting the internal array pointer.3 *4 * @param array $array5 * @return int|string|null6 */7function array_key_first(array $array)
array_key_last
1/**2 * Get the last key of the given array without affecting the internal array pointer.3 *4 * @param array $array5 * @return int|string|null6 */7function array_key_last(array $array)
array_map_assoc
1/**2 * Run a map over each of the items in the array.3 *4 * @param callable $callback5 * @param array $array6 * @return array7 */8function array_map_assoc(callable $callback, array $array): array
array_map_with_keys
1/**2 * Run a map over each of the items in the array.3 *4 * @param callable $callback5 * @param array $array6 * @return array7 */8function array_map_with_keys(callable $callback, array $array): array
array_undot
1/**2 * Expand a dotted array into a full multi-dimensional array.3 *4 * @param iterable $array5 * @return array6 */7function array_undot(iterable $array): array
array_value_first
1/**2 * Get the first value from an array.3 *4 * @param array $array5 * @param mixed $default6 * @return mixed7 */8function array_value_first(array $array, $default = null)
array_value_last
1/**2 * Get the last value from an array.3 *4 * @param array $array5 * @param mixed $default6 * @return mixed7 */8function array_value_last(array $array, $default = null)
to_array
1/**2 * Convert Json into Array.3 *4 * @param string $json5 *6 * @return array7 */8function to_array(string $json)
Strings
preg_replace_array
1/** 2 * Replace a given pattern with each value in the array in sequentially. 3 * 4 * @param string $pattern 5 * @param array $replacements 6 * @param string $subject 7 * 8 * @return string 9 */10function preg_replace_array(string $pattern, array $replacements, string $subject): string
str_after
1/**2 * Return the remainder of a string after the first occurrence of a given value.3 *4 * @param string $subject5 * @param string $search6 *7 * @return string8 */9function str_after(string $subject, string $search): string
str_after_last
1/**2 * Return the remainder of a string after the last occurrence of a given value.3 *4 * @param string $subject5 * @param string $search6 *7 * @return string8 */9function str_after_last(string $subject, string $search): string
str_before
1/**2 * Get the portion of a string before the first occurrence of a given value.3 *4 * @param string $subject5 * @param string $search6 *7 * @return string8 */9function str_before(string $subject, string $search): string
str_before_last
1/**2 * Get the portion of a string before the last occurrence of a given value.3 *4 * @param string $subject5 * @param string $search6 *7 * @return string8 */9function str_before_last(string $subject, string $search): string
str_between
1/** 2 * Get the portion of a string between two given values. 3 * 4 * @param string $subject 5 * @param string $from 6 * @param string $to 7 * 8 * @return string 9 */10function str_between(string $subject, string $from, string $to): string
str_contains
1 /**2 * Determine if a given string contains a given substring.3 *4 * @param string $haystack5 * @param string|string[] $needles6 *7 * @return bool8 */9function str_contains(string $haystack, $needles): bool
str_contains_all
1/**2 * Determine if a given string contains all array values.3 *4 * @param string $haystack5 * @param string[] $needles6 *7 * @return bool8 */9function str_contains_all(string $haystack, array $needles): bool
str_ends_with
1/**2 * Determine if a given string ends with a given substring.3 *4 * @param string $haystack5 * @param string|string[] $needles6 *7 * @return bool8 */9function str_ends_with(string $haystack, $needles): bool
str_finish
1/**2 * Cap a string with a single instance of a given value.3 *4 * @param string $value5 * @param string $cap6 *7 * @return string8 */9function str_finish(string $value, string $cap): string
str_is
1/**2 * Determine if a given string matches a given pattern.3 *4 * @param string|array $pattern5 * @param string $value6 *7 * @return bool8 */9function str_is($pattern, string $value): bool
str_is_uuid
1/**2 * Determine if a given string is a valid UUID.3 *4 * @param string $value5 *6 * @return bool7 */8function str_is_uuid(string $value): bool
str_kebab
1/**2 * Convert a string to kebab case.3 *4 * @param string $value5 *6 * @return string7 */8function str_kebab(string $value): string
str_length
1/**2 * Return the length of the given string.3 *4 * @param string $value5 * @param string|null $encoding6 *7 * @return int8 */9function str_length(string $value, ?string $encoding = null): int
str_limit
1/** 2 * Limit the number of characters in a string. 3 * 4 * @param string $value 5 * @param int $limit 6 * @param string $end 7 * 8 * @return string 9 */10function str_limit(string $value, int $limit = 100, string $end = '...'): string
str_lower
1/**2 * Convert the given string to lower-case.3 *4 * @param string $value5 *6 * @return string7 */8function str_lower(string $value): string
str_words
1/** 2 * Limit the number of words in a string. 3 * 4 * @param string $value 5 * @param int $words 6 * @param string $end 7 * 8 * @return string 9 */10function str_words(string $value, int $words = 100, string $end = '...'): string
str_match
1/**2 * Get the string matching the given pattern.3 *4 * @param string $pattern5 * @param string $subject6 *7 * @return string8 */9function str_match(string $pattern, string $subject): string
str_pad_both
1/** 2 * Pad both sides of a string with another. 3 * 4 * @param string $value 5 * @param int $length 6 * @param string $pad 7 * 8 * @return string 9 */10function str_pad_both(string $value, int $length, string $pad = ' '): string
str_pad_left
1/** 2 * Pad the left side of a string with another. 3 * 4 * @param string $value 5 * @param int $length 6 * @param string $pad 7 * 8 * @return string 9 */10function str_pad_left(string $value, int $length, string $pad = ' '): string
str_pad_right
1/** 2 * Pad the right side of a string with another. 3 * 4 * @param string $value 5 * @param int $length 6 * @param string $pad 7 * 8 * @return string 9 */10function str_pad_right(string $value, int $length, string $pad = ' '): string
str_random
1/**2 * Generate a more truly "random" alpha-numeric string.3 *4 * @param int $length5 *6 * @return string7 */8function str_random(int $length = 16): string
str_replace_array
1/** 2 * Replace a given value in the string sequentially with an array. 3 * 4 * @param string $search 5 * @param array<int|string, string> $replace 6 * @param string $subject 7 * 8 * @return string 9 */10function str_replace_array(string $search, array $replace, string $subject): string
str_replace_first
1/** 2 * Replace the first occurrence of a given value in the string. 3 * 4 * @param string $search 5 * @param string $replace 6 * @param string $subject 7 * 8 * @return string 9 */10function str_replace_first(string $search, string $replace, string $subject): string
str_replace_last
1/** 2 * Replace the last occurrence of a given value in the string. 3 * 4 * @param string $search 5 * @param string $replace 6 * @param string $subject 7 * 8 * @return string 9 */10function str_replace_last(string $search, string $replace, string $subject): string
str_remove
1/** 2 * Remove any occurrence of the given string in the subject. 3 * 4 * @param string|array<string> $search 5 * @param string $subject 6 * @param bool $caseSensitive 7 * 8 * @return string 9 */10function str_remove($search, string $subject, bool $caseSensitive = true): string
str_start
1/**2 * Begin a string with a single instance of a given value.3 *4 * @param string $value5 * @param string $prefix6 *7 * @return string8 */9function str_start(string $value, string $prefix): string
str_upper
1/**2 * Convert the given string to upper-case.3 *4 * @param string $value5 *6 * @return string7 */8function str_upper(string $value): string
str_title
1/**2 * Convert the given string to title case.3 *4 * @param string $value5 *6 * @return string7 */8function str_title(string $value): string
str_snake
1/**2 * Convert a string to snake case.3 *4 * @param string $value5 * @param string $delimiter6 *7 * @return string8 */9function str_snake(string $value, string $delimiter = '_'): string
str_starts_with
1/**2 * Determine if a given string starts with a given substring.3 *4 * @param string $haystack5 * @param string|string[] $needles6 *7 * @return bool8 */9function str_starts_with(string $haystack, $needles): bool
str_studly
1/**2 * Convert a value to studly caps case.3 *4 * @param string $value5 *6 * @return string7 */8function str_studly(string $value): string
str_pascal
1/**2 * Convert a string to pascal case.3 *4 * @param string $value5 *6 * @return string7 */8function str_pascal(string $value): string
str_camel
1/**2 * Convert a string to cameel case.3 *4 * @param string $value5 *6 * @return string7 */8function str_camel(string $value): string
str_uuid4
1/**2 * Generate a UUID (version 4).3 *4 * @return string5 */6function str_uuid4(): string
str_ascii
1/**2 * Transliterate a UTF-8 value to ASCII.3 *4 * @param string $value5 * @param string $language6 * @return string7 */8function str_ascii(string $value, string $language = 'en'): string
str_slug
1/**2 * Generate a URL friendly "slug" from a given string.3 *4 * @param string $title5 * @param string $separator6 * @param string|null $language7 * @return string8 */9function str_slug(string $title, string $separator = '-', ?string $language = 'en'): string
str_plural
1/**2 * Get the plural form of an English word.3 *4 * @param string $value5 * @param int|array|\Countable $count6 * @return string7 */8function str_plural(string $value, $count = 2): string
str_singular
1/**2 * Get the singular form of an English word.3 *4 * @param string $value5 * @return string6 */7function str_singular(string $value): string
str_ucfirst
1/**2 * Make a string's first character uppercase.3 *4 * @param string $string5 * @return string6 */7function str_ucfirst(string $string): string
str_lcfirst
1/**2 * Make a string's first character lowercase.3 *4 * @param string $string5 * @return string6 */7function str_lcfirst(string $string): string
str_mask
1/** 2 * Mask a portion of a string with a repeated character. 3 * 4 * @param string $string 5 * @param string $character 6 * @param int $index 7 * @param int|null $length 8 * @return string 9 */10function str_mask(string $string, string $character, int $index, ?int $length = null): string
str_contains_any
1/**2 * Determine if a given string contains any of the given substrings.3 *4 * @param string $haystack5 * @param string[] $needles6 * @return bool7 */8function str_contains_any(string $haystack, array $needles): bool
str_excerpt
1/**2 * Extract an excerpt from text that matches the first instance of a phrase.3 *4 * @param string $text5 * @param string $phrase6 * @param array $options7 * @return string|null8 */9function str_excerpt(string $text, string $phrase = '', array $options = []): ?string
str_headline
1/**2 * Convert the given string to title case for each word.3 *4 * @param string $value5 * @return string6 */7function str_headline(string $value): string
str_is_ascii
1/**2 * Determine if a given string is 7 bit ASCII.3 *4 * @param string $value5 * @return bool6 */7function str_is_ascii(string $value): bool
str_is_json
1/**2 * Determine if a given string is valid JSON.3 *4 * @param string $value5 * @return bool6 */7function str_is_json(string $value): bool
str_password
1/**2 * Generate a more truly "random" alpha-numeric string.3 *4 * @param int $length5 * @return string6 */7function str_password(int $length = 32): string
str_reverse
1/**2 * Reverse the given string.3 *4 * @param string $value5 * @return string6 */7function str_reverse(string $value): string
str_squish
1/**2 * Remove all "extra" blank space from the given string.3 *4 * @param string $value5 * @return string6 */7function str_squish(string $value): string
str_swap
1/**2 * Swap multiple values in a string with other values.3 *4 * @param array $map5 * @param string $subject6 * @return string7 */8function str_swap(array $map, string $subject): string
str_wrap
1/**2 * Wrap a string to a given number of characters.3 *4 * @param string $value5 * @param int $width6 * @param string $break7 * @return string8 */9function str_wrap(string $value, int $width = 75, string $break = "\n"): string
str_jwt
1/**2 * Generate a JWT.3 *4 * @param array $payload5 *6 * @return string7 */8function str_jwt(array $payload): string
Classes
class_basename
1/**2 * Get the class "basename" of the given object / class.3 *4 * @param string|object $class5 * @return string6 */7function class_basename($class): string
class_uses_recursive
1/**2 * Returns all traits used by a class, its parent classes and trait of their traits.3 *4 * @param object|string $class5 * @return array6 */7function class_uses_recursive($class): array
trait_uses_recursive
1/**2 * Returns all traits used by a trait and its traits.3 *4 * @param string $trait5 * @return array6 */7function trait_uses_recursive($trait): array
General Helpers
dd
1/**2 * Dump the passed variables and end the script.3 *4 * @param mixed ...$vars5 * @return never6 */7function dd(...$vars)
dump
1/**2 * Dump the passed variables.3 *4 * @param mixed ...$vars5 * @return mixed6 */7function dump(...$vars)
str
1/**2 * Get a string helper instance or return the string.3 *4 * @param string|null $string5 * @return string|object6 */7function str($string = null)
blank
1/**2 * Determine if the given value is "blank".3 *4 * @param mixed $value5 * @return bool6 */7function blank($value)
filled
1/**2 * Determine if a value is "filled".3 *4 * @param mixed $value5 * @return bool6 */7function filled($value)
throw_if
1/** 2 * Throw the given exception if the given condition is true. 3 * 4 * @param mixed $condition 5 * @param \Throwable|string $exception 6 * @param array ...$parameters 7 * @return mixed 8 * 9 * @throws \Throwable10 */11function throw_if($condition, $exception, ...$parameters)
throw_unless
1/** 2 * Throw the given exception unless the given condition is true. 3 * 4 * @param mixed $condition 5 * @param \Throwable|string $exception 6 * @param array ...$parameters 7 * @return mixed 8 * 9 * @throws \Throwable10 */11function throw_unless($condition, $exception, ...$parameters)
transform
1/**2 * Transform the given value if it is present.3 *4 * @param mixed $value5 * @param callable $callback6 * @param mixed $default7 * @return mixed|null8 */9function transform($value, callable $callback, $default = null)
windows_os
1/**2 * Determine if the current environment is Windows based.3 *4 * @return bool5 */6function windows_os()
retry
1/** 2 * Retry an operation a given number of times. 3 * 4 * @param int $times 5 * @param callable $callback 6 * @param int|\Closure $sleepMilliseconds 7 * @param callable|null $when 8 * @return mixed 9 *10 * @throws \Throwable11 */12function retry($times, callable $callback, $sleepMilliseconds = 0, $when = null)
rescue
1/**2 * Catch a potential exception and return a default value.3 *4 * @param callable $callback5 * @param mixed $rescue6 * @param bool $report7 * @return mixed8 */9function rescue(callable $callback, $rescue = null, $report = true)
e
1/**2 * Escape HTML entities in a string.3 *4 * @param string $value5 *6 * @return string7 */8function e(string $value): string
object_get
1 /** 2 * Get an item from an object using "dot" notation. 3 * 4 * @param object $object 5 * @param string $key 6 * @param mixed $default 7 * 8 * @return mixed 9 */10function object_get(object $object, string $key, $default = null)
tap
1/**2 * Call the given Closure with the given value then return the value.3 *4 * @param mixed $value5 * @param callable $callback6 *7 * @return mixed8 */9function tap($value, callable $callback)
value
1/**2 * Return the default value of the given value.3 *4 * @param mixed $value5 * @return mixed6 */7function value($value)
with
1/**2 * Return the given object. Useful for chaining.3 *4 * @param mixed $object5 * @return mixed6 */7function with($object)