PHPackages                             jasny/php-functions - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. jasny/php-functions

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

jasny/php-functions
===================

A set of useful PHP functions

v4.1.0(7y ago)44529.2k—6.9%9[1 issues](https://github.com/jasny/php-functions/issues)9MITPHPPHP &gt;=7.1

Since Sep 14Pushed 1y ago5 watchersCompare

[ Source](https://github.com/jasny/php-functions)[ Packagist](https://packagist.org/packages/jasny/php-functions)[ RSS](/packages/jasny-php-functions/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (1)Versions (24)Used By (9)

Jasny's PHP functions
=====================

[](#jasnys-php-functions)

[![Build Status](https://camo.githubusercontent.com/c05709119fa42d09a986a95435c81360fddf6ec5cfa065b58a35b38f3a33cb35/68747470733a2f2f7472617669732d63692e6f72672f6a61736e792f7068702d66756e6374696f6e732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/jasny/php-functions)[![Code Coverage](https://camo.githubusercontent.com/a1259fb1fa009df890fcc980b60efb1c6db6057907cf98a8803053927dc59313/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a61736e792f7068702d66756e6374696f6e732f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jasny/php-functions/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/f89aadf30b43c857e9c95cdd330e670ce76e5b2d01fa2ec6d0de5fcc0e1e8a1b/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a61736e792f7068702d66756e6374696f6e732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jasny/php-functions/?branch=master)[![SensioLabsInsight](https://camo.githubusercontent.com/0686d138e96afb1142b06eb6bfa3dca1d22178cc23ecb77324f094c20ba53011/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f37396633656531382d653866632d346337662d386239372d3335643034633437613635632f6d696e692e706e67)](https://insight.sensiolabs.com/projects/79f3ee18-e8fc-4c7f-8b97-35d04c47a65c)[![Packagist Stable Version](https://camo.githubusercontent.com/6e18be3637a3473740a9909ff811c0c68d7558a8e379c36b4c3e74a1813076fc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a61736e792f7068702d66756e6374696f6e732e737667)](https://packagist.org/packages/jasny/php-functions)[![Packagist License](https://camo.githubusercontent.com/5094d8692fabec5ce129586f4c37d800ff845662479599b5c51d408e3e90b93b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6a61736e792f7068702d66756e6374696f6e732e737667)](https://packagist.org/packages/jasny/php-functions)

A set PHP functions that *should* have been part of PHP's core libraries.

**Example**

```
$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...

[](#but-wait-theres-more)

If you like these functions, you'll love the [Improved PHP library](https://github.com/improved-php-library). Go and check it out.

Installation
------------

[](#installation)

```
composer require jasny\php-functions

```

Usage
-----

[](#usage)

**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
```

To import all the functions to the global namespace require 'global.php' anywhere in your application.

```
require_once 'vendor/jasny/php-functions/global.php';
```

Alternatively, add it to the `autoload` section of `composer.json`.

```
"autoload": {
    "files": [
        "vendor/jasny/php-functions/global.php"
    ]
}

```

Type functions
--------------

[](#type-functions)

#### is\_associative\_array

[](#is_associative_array)

```
boolean is_associative_array(mixed $var)

```

Check if variable is an associative array.

#### is\_numeric\_array

[](#is_numeric_array)

```
boolean is_numeric_array(mixed $var)

```

Check if variable is a numeric array.

#### is\_stringable

[](#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.

#### objectify

[](#objectify)

```
stdClass|mixed objectify(array|mixed $var)

```

Turn an associated array into a `stdClass` object recursively.

#### arrayify

[](#arrayify)

```
array|mixed arrayify(stdClass|mixed $var)

```

Turn an `stdClass` object into an associated array recursively.

#### get\_type\_description

[](#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".

#### expect\_type

[](#expect_type)

```
expect_type(mixed $var, string|string[] $type, string $throwable = null, string $message = null)

```

Validate that an argument has a specific type.

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.*

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.

The message may contain a `%s`, which is replaced by the type of `$var`.

###### Example

[](#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-functions)

#### array\_only

[](#array_only)

```
array array_only(array $array, array $keys)

```

Return an array with only the specified keys.

#### array\_without

[](#array_without)

```
array array_without(array $array, array $keys)

```

Return an array without the specified keys.

#### array\_contains\_all

[](#array_contains_all)

```
boolean array_contains_all(array $array, array $subset, boolean $strict = false)

```

Check if an array contains all values in a set.

*This function works as expected with nested arrays or an array with objects.*

#### array\_contains\_all\_assoc

[](#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.

*This function works as expected with nested arrays or an array with objects.*

#### array\_contains\_any

[](#array_contains_any)

```
boolean array_contains_any(array $array, array $subset, boolean $strict = false)

```

Check if an array contains any value in a set.

*This function works as expected with nested arrays or an array with objects.*

#### array\_contains\_any\_assoc

[](#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.

*This function works as expected with nested arrays or an array with objects.*

#### array\_find

[](#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.

Flag determining what arguments are sent to callback:

- `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

[](#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.

#### array\_flatten

[](#array_flatten)

```
array function array_flatten(string $glue, array $array)

```

Flatten a nested associative array, concatenating the keys.

###### Example

[](#example-1)

```
$values = array_flatten('.', [
    'animal' => [
        'mammel' => [
            'ape',
            'bear'
        ],
        'reptile' => 'chameleon'
    ],
    'colors' => [
        'red' => 60,
        'green' => 100,
        'blue' => 0
    ]
]);
```

Will become

```
[
    'animal.mammel' => [
        'ape',
        'bear'
    ],
    'animal.reptile' => 'chameleon',
    'colors.red' => 60,
    'colors.green' => 100,
    'colors.blue' => 0
]
```

#### array\_join\_pretty

[](#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.

###### Example

[](#example-2)

```
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
----------------

[](#string-functions)

#### str\_starts\_with

[](#str_starts_with)

```
boolean str_starts_with(string $string, $string $substr)

```

Check if a string starts with a substring.

#### str\_ends\_with

[](#str_ends_with)

```
boolean str_ends_with(string $string, string $substr)

```

Check if a string ends with a substring.

#### str\_contains

[](#str_contains)

```
boolean str_contains(string $string, string $substr)

```

Check if a string contains a substring.

#### str\_before

[](#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.

#### str\_after

[](#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.

#### str\_remove\_accents

[](#str_remove_accents)

```
string str_remove_accents(string $string)

```

Replace characters with accents with normal characters.

#### str\_slug

[](#str_slug)

```
string str_slug(string $string, string $glue = '-')

```

Generate a URL friendly slug from the given string.

Cast functions
--------------

[](#cast-functions)

#### camelcase

[](#camelcase)

```
string camelcase(string $string)

```

Turn a sentence, StudlyCase, snake\_case or kabab-case into **camelCase**.

#### studlycase

[](#studlycase)

```
string studlycase(string $string, $ucfirst = true)

```

Turn a sentence, camelCase, snake\_case or kabab-case into **StudlyCase**.

#### snakecase

[](#snakecase)

```
string snakecase(string $string)

```

Turn a sentence, StudlyCase, camelCase or kabab-case into **snake\_case**.

#### kababcase

[](#kababcase)

```
string kababcase(string $string)

```

Turn a sentence, StudlyCase, camelCase or snake\_case into **kabab-case**.

#### uncase

[](#uncase)

```
string uncase(string $string)

```

Turn StudlyCase, camelCase, snake\_case or kabab-case into **a sentence**.

Server functions
----------------

[](#server-functions)

#### ip\_in\_cidr

[](#ip_in_cidr)

```
boolean ip_in_cidr(string $ip, string $cidr)

```

Check if an IP address is in a [CIDR](https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing) block.

*Works with IPv4 and IPv6.*

#### ipv4\_in\_cidr

[](#ipv4_in_cidr)

```
boolean ipv4_in_cidr(string $ip, string $cidr)

```

Check if an IPv4 address is in a CIDR block.

#### ipv6\_in\_cidr

[](#ipv6_in_cidr)

```
boolean ipv6_in_cidr(string $ip, string $cidr)

```

Check if an IPv6 address is in a CIDR block.

#### inet\_to\_bits

[](#inet_to_bits)

```
string inet_to_bits(string $inet)

```

Converts inet\_pton output to string with bits.

File functions
--------------

[](#file-functions)

#### file\_contains

[](#file_contains)

```
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\_extended

[](#fnmatch_extended)

```
boolean fnmatch_extended(string $pattern, string $path)

```

Match path against wildcard pattern. This is an extended version of [fnmatch](http://php.net/fnmatch).

- `?` 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
---------------------------

[](#function-handling-functions)

#### call\_user\_func\_assoc

[](#call_user_func_assoc)

```
mixed call_user_func_assoc(callable $callback, array $param_arr)

```

Call a callback with named parameters as associative array.

Object functions
----------------

[](#object-functions)

#### object\_get\_properties

[](#object_get_properties)

```
array object_get_properties(object $object, bool $dynamic = true)

```

Get the public properties of an object.

Unlike `get_object_vars`, this method will return only public properties regardless of the scope.

The `dynamic` flag controls if the output should be filtered, so only properties defined in the class are set.

#### object\_set\_properties

[](#object_set_properties)

```
array object_get_properties(object $object, array $data, bool $dynamic = true)

```

Set the public properties of an object.

The `dynamic` flag controls if `$data` should be filtered, so only properties defined in the class are set.

###  Health Score

45

—

FairBetter than 93% of packages

Maintenance28

Infrequent updates — may be unmaintained

Popularity48

Moderate usage in the ecosystem

Community21

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~82 days

Recently: every ~160 days

Total

23

Last Release

2092d ago

Major Versions

v2.2.1 → v3.0.02017-01-28

v2.x-dev → v4.0.02018-09-06

v1.0.0 → v3.3.12018-11-26

PHP version history (4 changes)v2.0.1PHP &gt;=5.5

v3.0.0PHP &gt;=5.6

v4.0.0PHP &gt;=7.1

v2.0.0PHP &gt;=5.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/3379a93d51305df325df9045e1a8b205d195e4e8c01312dff53a000ee79002eb?d=identicon)[jasny](/maintainers/jasny)

---

Top Contributors

[![jasny](https://avatars.githubusercontent.com/u/100821?v=4)](https://github.com/jasny "jasny (83 commits)")

### Embed Badge

![Health badge](/badges/jasny-php-functions/health.svg)

```
[![Health](https://phpackages.com/badges/jasny-php-functions/health.svg)](https://phpackages.com/packages/jasny-php-functions)
```

###  Alternatives

[unclecheese/betterbuttons

Adds new form actions and buttons to GridField detail form for usability enhancements.

85544.9k19](/packages/unclecheese-betterbuttons)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
