napryc / php8-compat
PHP 8 compatibility layer for PHP 7.1+ applications
v1.1.0
2025-02-19 15:16 UTC
Requires
- php: ^7.1 || ^8.0
- ext-mbstring: *
Requires (Dev)
- phpunit/phpunit: ^7.0 || ^8.0 || ^9.0
This package is auto-updated.
Last update: 2025-08-19 16:31:23 UTC
README
A lightweight compatibility layer that brings PHP 8.0+ string and array functions to PHP 7.1+ applications.
Features
- Multi-byte string functions support
- Array manipulation utilities
- Zero dependencies (except ext-mbstring)
- PHP 7.1+ compatible
- Automatic fallback to native PHP 8 functions when available
Installation
Install via Composer:
composer require napryc/php8-compat
Available Functions
String Functions
str_contains()
- Determine if a string contains a given substringstr_starts_with()
- Check if a string starts with a given substringstr_ends_with()
- Check if a string ends with a given substringstr_increment()
- Increment a stringstr_decrement()
- Decrement a stringmb_str_pad()
- Pad a string to a certain length with another stringmb_trim()
- Strip whitespace or other characters from both sidesmb_ltrim()
- Strip whitespace or other characters from the left sidemb_rtrim()
- Strip whitespace or other characters from the right sidemb_ucfirst()
- Make a string's first character uppercasemb_lcfirst()
- Make a string's first character lowercase
Array Functions
array_is_list()
- Determine if an array is a listarray_find()
- Find first element matching callbackarray_find_key()
- Find first key matching callbackarray_any()
- Check if any element matches callbackarray_all()
- Check if all elements match callback
Math Functions
fdiv()
- Divide two numbers with proper handling of division by zero
JSON Functions
json_validate()
- Validate a JSON string
Usage
use Napryc\PHP8Compat\PHP8Compat; // String manipulation $padded = PHP8Compat::mb_str_pad("Hello", 10, "-"); // "Hello-----" $contains = PHP8Compat::str_contains("Hello World", "World"); // true $starts = PHP8Compat::str_starts_with("Hello World", "Hello"); // true $ends = PHP8Compat::str_ends_with("Hello World", "World"); // true // String increment/decrement $next = PHP8Compat::str_increment("a"); // "b" $prev = PHP8Compat::str_decrement("b"); // "a" // Multi-byte string functions $trimmed = PHP8Compat::mb_trim(" Hello "); // "Hello" // Array checking $isList = PHP8Compat::array_is_list([1, 2, 3]); // true // Array searching $found = PHP8Compat::array_find([1, 2, 3], fn($x) => $x > 1); // 2 $foundKey = PHP8Compat::array_find_key([1, 2, 3], fn($x) => $x > 1); // 1 $hasAny = PHP8Compat::array_any([1, 2, 3], fn($x) => $x > 2); // true $allMatch = PHP8Compat::array_all([1, 2, 3], fn($x) => $x > 0); // true // Safe division $result = PHP8Compat::fdiv(10, 0); // INF
Requirements
- PHP 7.1 or higher
- ext-mbstring extension
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.