ShortPHP Utilities

Utilities add small, reusable behaviours rather than merely renaming PHP functions. Every utility has documentation and a working usage example.

search
UTILITY contains()

Arrays

Test whether an array contains a value.

UTILITY unique()

Arrays

Remove duplicate values while preserving first occurrence order.

UTILITY flatten()

Arrays

Flatten nested arrays to a chosen depth.

UTILITY find()

Arrays

Return the first array item matching a callback.

UTILITY find_key()

Arrays

Return the key of the first matching array item.

UTILITY pluck()

Arrays

Extract one field from a list of arrays or objects.

UTILITY partition()

Arrays

Split an array into matching and non-matching items.

UTILITY sort_by()

Arrays

Sort records by a field or property.

UTILITY set_path()

Data

Set a nested array value using dot notation.

UTILITY has_path()

Data

Check whether a nested dot-notation path exists.

UTILITY forget()

Data

Remove a nested dot-notation array value.

UTILITY replace_first()

Strings

Replace only the first literal occurrence.

UTILITY replace_last()

Strings

Replace only the last literal occurrence.

UTILITY remove()

Strings

Remove one or more literal strings.

UTILITY collapse_spaces()

Strings

Collapse whitespace runs to one space.

UTILITY words()

Strings

Return at most N whitespace-separated words.

UTILITY word_count()

Strings

Count whitespace-separated words.

UTILITY excerpt()

Strings

Create a compact, word-aware excerpt.

UTILITY initials()

Strings

Create initials from a name.

UTILITY camel()

Strings

Convert text to camelCase.

UTILITY snake()

Strings

Convert text to snake_case.

UTILITY kebab()

Strings

Convert text to kebab-case.

UTILITY mask()

Security

Mask a string for presentation while retaining selected ends.

UTILITY clamp()

Numbers

Keep a number inside an inclusive range.

UTILITY is_even()

Numbers

Test whether an integer is even.

UTILITY is_odd()

Numbers

Test whether an integer is odd.

UTILITY to_bool()

Validation

Convert common human-readable boolean values.

UTILITY age()

Dates

Calculate whole calendar years from a birth date.

UTILITY ago()

Dates

Format a timestamp as relative time.

UTILITY days_between()

Dates

Return signed whole calendar days between dates.

UTILITY url_host()

URLs

Extract the host component of a URL.

UTILITY url_path()

URLs

Extract the path component of a URL.

UTILITY url_with()

URLs

Add, replace or remove query parameters.

UTILITY env_bool()

Environment

Read an environment variable as a boolean.

UTILITY env_int()

Environment

Read an environment variable as an integer.

UTILITY env_float()

Environment

Read an environment variable as a float.

UTILITY redact()

Security

Recursively replace sensitive array values.

UTILITY php_at_least()

PHP Runtime

Check whether the running PHP version meets a minimum.

UTILITY extension_available()

PHP Runtime

Check whether a PHP extension is loaded.

UTILITY timer_start()

Debugging

Start a high-resolution elapsed-time measurement.

UTILITY timer_end()

Debugging

Return elapsed seconds since timer_start().

UTILITY dd()

Debugging

Dump values and terminate execution. Use only while debugging.

UTILITY json_try_decode()

JSON

Decode JSON without throwing; return the supplied default on invalid JSON.

UTILITY json_try_encode()

JSON

Encode JSON without throwing; return the supplied default on failure.

UTILITY http_get()

HTTP

Perform a GET request with cURL and return status, headers and body.

UTILITY http_post()

HTTP

Perform a POST request with cURL.

UTILITY http_put()

HTTP

Perform a PUT request with cURL.

UTILITY http_patch()

HTTP

Perform a PATCH request with cURL.

UTILITY http_delete()

HTTP

Perform a DELETE request with cURL.