Install JBZoo / Utils via Composer
masterTo use this collection of PHP functions and utility classes in your project, install it using Composer:
composer require jbzoo/utilscomposer require jbzoo/utilsrepository·master·Indexed 21 days ago
https://github.com/jbzoo/utilsA collection of PHP functions, mini classes, and snippets for routine developer tasks. It includes utilities for array manipulation (Arr), CLI interaction (Cli), CSV parsing (Csv), date handling (Dates), email validation (Email), environment variables (Env), filesystem management (FS), data filtering (Filter), HTTP headers (Http), IP networking (IP), image processing (Image), serialization (Ser), slug generation (Slug), statistical calculations (Stats), string manipulation (Str), and system environment checks (Sys).
To use this collection of PHP functions and utility classes in your project, install it using Composer:
composer require jbzoo/utilscomposer require jbzoo/utilsTo ensure the code quality and run the full suite of unit tests, use the provided make commands in your terminal.
make
make test-allJBZoo provides several standalone smart functions for quick type casting and string cleaning. These are useful for normalizing input data.
Type Conversions:
int($value): Converts to integer (e.g., ' 10.0 ' becomes 10).float($value): Converts to float (e.g., ' 10.0 ' becomes 10.0).bool($value): Converts truthy/falsy strings like 'yes', 'no', '1', or '0' to boolean.String Cleaning:
alias($string): Creates a URL-friendly alias (e.g., 'Qwer ty' becomes 'qwer-ty').digits($string): Extracts only digits.alpha($string): Extracts only alphabetic characters.alphanum($string): Extracts only alphanumeric characters.use function JBZoo//Utils//alias;
use function JBZoo//Utils//alpha;
use function JBZoo//Utils//alphanum;
use function JBZoo//Utils//bool;
use function JBZoo//Utils//digits;
use function JBZoo//Utils//float;
use function JBZoo//Utils//int;
int(' 10.0 ') === 10;
float(' 10.0 ') === 10.0;
bool(' yes ') === true;
bool(' no ') === false;
alias('Qwer ty') === 'qwer-ty';
digits('Qwer 1 ty2') === '12';
alpha('Qwer 1 ty2') === 'Qwerty';
alphanum(' #$% Qwer 1 ty2') === 'Qwer1ty2';The Vars class provides mathematical utility functions for numbers.
Key Methods:
Vars::isEven(int $number) / isOdd: Checks parity.Vars::isIn(float $number, float $min, float $max): Checks if a number is within a range.Vars::range(float $value, float $min, float $max): Ensures a value is clamped between $min and $max.Vars::limit(float $number, float $min, float $max): Limits a number between two bounds.The Ser class provides tools for managing PHP serialized data, including fixing corrupted strings.
Key Methods:
Ser::fix(string $brokenSerializedData): Attempts to fix partially-corrupted serialized arrays (often caused by character set mismatches).Ser::is(?mixed $data): Checks if the data is a serialized string.Ser::maybe(?mixed $data): Serializes data only if it is not already serialized.Ser::maybeUn(string $data): Unserializes a value only if it is a serialized string.The Slug class helps create URL-safe strings.
Key Methods:
Slug::filter(?string $string, string $separator = '-', bool $cssMode = false): Converts accent characters to ASCII, replaces non-alphanumeric characters with separators, and collapses multiple separators. If $cssMode is true, it creates strings safe for CSS classes/IDs.Slug::removeAccents(string $string, string $language = ''): Converts accent characters to their ASCII equivalents.Slug::seemsUTF8(string $string): Checks if a string is UTF-8 encoded.The Http class provides helpers for common HTTP response tasks.
Key Methods:
Http::download(string $filename): Transmits headers to force a browser download dialog.Http::nocache(): Sets headers to prevent browser caching.Http::utf8(string $contentType = 'text/html'): Transmits UTF-8 content headers.Http::getHeaders(): Retrieves all current HTTP headers.The Env class provides type-safe access to environment variables.
Key Methods:
Env::get(?string $envVarName, ?string|int|float|bool|null $default = null, int $options = 16): Retrieves an environment variable.Env::bool(string $envVarName, bool $default = false): Converts an env var to a strict boolean.Env::int(string $envVarName, int $default = 0): Converts an env var to a strict integer.Env::float(string $envVarName, float $default = 0): Converts an env var to a strict float.Env::string(string $envVarName, string $default = ''): Converts an env var to a clean string.Env::isExists(string $envVarName): Checks if the variable exists.The Xml class allows for converting between PHP arrays and \DOMDocument objects.
Key Methods:
Xml::array2Dom(array $xmlAsArray, ?\DOMElement $domElement = null, ?\DOMDocument $document = null): Converts a structured array into a \DOMDocument.Xml::dom2Array(\DOMNode $element): Converts a \DOMNode into a simple array.Xml::createFromString(?string $source = null, bool $preserveWhiteSpace = false): Creates a \DOMDocument from an XML string.The Str class is a powerful utility for string manipulation, including case conversion, truncation, and pattern matching.
Key Methods:
Str::clean(string $string, bool $toLower = false, bool $addSlashes = false, bool $removeAccents = true): Makes a string safe by removing UTF-8 chars, tags, and trimming.Str::uuid(): Generates a RFC 4122 compliant UUID v4.Str::limitChars(string $string, int $limit = 100, string $append = '...'): Truncates a string by character count.Str::limitWords(string $string, int $limit = 100, string $append = '...'): Truncates a string by word count.Str::slug(string $text = '', bool $isCache = false): Converts text to a slug.Str::random(int $length = 10, bool $isReadable = true): Generates a random string.Str::splitCamelCase(string $input, string $separator = '_', bool $toLower = true): Converts camelCase to human-readable format.The Image class provides utilities for working with GD image resources and image metadata.
Key Methods:
Image::checkGD(bool $throwException = true): Checks if the GD library is enabled.Image::isPng(?string $format = null) / isJpeg / isGif / isWebp: Checks the image format.Image::quality(float $percent): Returns a valid value for image quality (0..100).Image::rotate(float $color): Returns a valid value for image rotation (-360..360).The Dates class simplifies date creation, formatting, and relative time checks.
Key Methods:
Dates::factory(?mixed $time = null, ?DateTimeZone|string|null $timeZone = null): Creates a \DateTime object from various inputs.Dates::human(string|int $date, string $format = 'd M Y H:i'): Converts a date string or timestamp to a human-readable format.Dates::isToday(string|int $time) / isTomorrow / isYesterday: Checks if a given time matches today, tomorrow, or yesterday.Dates::isThisMonth(string|int $time) / isThisWeek / isThisYear: Checks if a date falls within the current month, week, or year.Dates::sql(?string|int|null $time = null): Converts a time to a SQL-compatible format.