PHPackages                             andreas-glaser/php-helpers - 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. andreas-glaser/php-helpers

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

andreas-glaser/php-helpers
==========================

A comprehensive collection of PHP utility functions for array manipulation, string operations, date handling, HTML generation, form building, validation, and more. Modern PHP 8.2+ library with full type safety.

v2.0.0(11mo ago)1386.5k↓41.7%22MITPHPPHP ^8.2|^8.3CI passing

Since Jan 6Pushed 9mo ago1 watchersCompare

[ Source](https://github.com/andreas-glaser/php-helpers)[ Packagist](https://packagist.org/packages/andreas-glaser/php-helpers)[ Docs](https://github.com/andreas-glaser/php-helpers)[ RSS](/packages/andreas-glaser-php-helpers/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)Dependencies (2)Versions (31)Used By (2)

PHP Helpers
===========

[](#php-helpers)

A comprehensive collection of PHP helper functions and classes to simplify common programming tasks.

Requirements
------------

[](#requirements)

- PHP ^8.2|^8.3
- ext-mbstring
- ext-json

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

[](#installation)

```
composer require andreas-glaser/php-helpers
```

Features
--------

[](#features)

### Array Helper (`ArrayHelper.php`)

[](#array-helper-arrayhelperphp)

A powerful utility class for array manipulation and operations.

#### Key Functions:

[](#key-functions)

- `ArrayHelper::get(array $array, $key, $default = null)`: Get value by key with default fallback
- `ArrayHelper::getKeyByValue(array $array, $value, $default = null, $strict = true)`: Get first key with matching value
- `ArrayHelper::getByPath(array $array, $path, $throwException = false, $default = null)`: Get value using dot notation path
- `ArrayHelper::setByPath(array $array, $path, $value)`: Set value using dot notation path
- `ArrayHelper::unsetByPath(array $array, string $path)`: Remove value using dot notation path
- `ArrayHelper::existsByPath(array $array, $path)`: Check if path exists
- `ArrayHelper::issetByPath(array $array, $path)`: Check if path is set
- `ArrayHelper::prepend(array $array, $value, $key = false)`: Add element at array start
- `ArrayHelper::append(array $array, $value, $key = false)`: Add element at array end
- `ArrayHelper::insertBefore(array &$array, $position, array $values)`: Insert values before position
- `ArrayHelper::insertAfter(array &$array, $position, array $values)`: Insert values after position
- `ArrayHelper::getFirstValue(array $array, $default = null)`: Get first array value
- `ArrayHelper::getLastValue(array $array, $default = null)`: Get last array value
- `ArrayHelper::getRandomValue(array $array)`: Get random array value
- `ArrayHelper::removeFirstElement(array $array)`: Remove first element
- `ArrayHelper::removeLastElement(array $array)`: Remove last element
- `ArrayHelper::removeByValue(array $array, $value, $strict = true)`: Remove element by value
- `ArrayHelper::keysCamelToUnderscore(array $array)`: Convert camelCase keys to snake\_case
- `ArrayHelper::unsetEmptyValues(array $array, $recursive = false)`: Remove empty values
- `ArrayHelper::implodeIgnoreEmpty($glue, array $pieces)`: Join array elements ignoring empty values
- `ArrayHelper::implodeKeys(string $glue, array $array)`: Join array keys with delimiter
- `ArrayHelper::explodeIgnoreEmpty(string $delimiter, string $string)`: Split string ignoring empty values
- `ArrayHelper::valueToUpper(array $array, $recursive = true)`: Convert values to uppercase
- `ArrayHelper::isAssoc(array $array)`: Check if array is associative
- `ArrayHelper::assocIndexesExist(array $arrayToCheck, array $arrayToCompareWith, $throwException = true)`: Check if indexes exist
- `ArrayHelper::replaceValue(array $array, $value, $replacement, $recursively = true, $caseSensitive = true)`: Replace values in array
- `ArrayHelper::merge(array $array1, array $array2)`: Merge arrays with proper handling of numeric keys
- `ArrayHelper::getFirstIndex($array, $default = null)`: Get first array index
- `ArrayHelper::unshiftAssoc($array, $key, $val)`: Add element at start with key
- `ArrayHelper::removeFirstIndex(array $array)`: Remove first array index

### String Helper (`StringHelper.php`)

[](#string-helper-stringhelperphp)

Comprehensive string manipulation and comparison utilities.

#### Key Functions:

[](#key-functions-1)

- `StringHelper::is($string, $stringToMatch, $caseSensitive = true)`: Compare strings with case sensitivity option
- `StringHelper::isOneOf($string, array $stringsToCompare, $caseSensitive = true)`: Check if string matches any in array
- `StringHelper::contains($haystack, $needle, $caseSensitive = true, $encoding = 'UTF-8')`: Check if string contains substring
- `StringHelper::startsWith($haystack, $needle, $caseSensitive = true, $encoding = 'UTF-8')`: Check if string starts with prefix
- `StringHelper::endsWith($haystack, $needle, $caseSensitive = true, $encoding = 'UTF-8')`: Check if string ends with suffix
- `StringHelper::trimMulti($string, array $chars)`: Remove multiple characters from both ends
- `StringHelper::lTrimMulti($string, array $chars)`: Remove multiple characters from left end
- `StringHelper::rTrimMulti($string, array $chars)`: Remove multiple characters from right end
- `StringHelper::camelToUnderscore($string)`: Convert camelCase to underscore\_case
- `StringHelper::removeLineBreaks($string, $replaceWith = ' ')`: Remove line breaks
- `StringHelper::removeRedundantWhiteSpaces($string)`: Remove redundant whitespace
- `StringHelper::replaceWhiteSpacesWithUnderscores($string)`: Replace spaces with underscores
- `StringHelper::machineReadable($string)`: Convert to machine-readable format
- `StringHelper::append($string, $stringToAppend)`: Append string
- `StringHelper::prepend($string, $stringToPrepend)`: Prepend string
- `StringHelper::removeChar($string, $char)`: Remove single character
- `StringHelper::removeChars($string, array $chars)`: Remove multiple characters
- `StringHelper::explodeAndTrim($delimiter, $string)`: Split string and trim each part
- `StringHelper::replace($subject, array $replacementMap, $caseSensitive = true)`: Replace multiple values
- `StringHelper::limitWords($str, $limit = 100, $end_char = null)`: Limit string to word count
- `StringHelper::limitChars($str, $limit = 100, $end_char = null, $preserve_words = false)`: Limit string to character count
- `StringHelper::getIncrementalId($prefix = '__undefined__')`: Generate sequential ID
- `StringHelper::isBlank($string)`: Check if string is empty or contains only whitespace
- `StringHelper::removeFromStart($string, $stringToRemove, $caseSensitive = true, $encoding = 'UTF-8')`: Remove prefix
- `StringHelper::removeFromEnd($string, $stringToRemove, $caseSensitive = true, $encoding = 'UTF-8')`: Remove suffix
- `StringHelper::linesToArray($string)`: Convert string with line breaks to array
- `StringHelper::__($string, array $params = null)`: String translation helper

### Date Helper (`DateHelper.php`)

[](#date-helper-datehelperphp)

Date and time manipulation and validation utilities.

#### Key Functions:

[](#key-functions-2)

- `DateHelper::isDateTime($date, $format = null)`: Check if value is valid datetime
- `DateHelper::stringToDateTime($string, $timezone = null, $null = null)`: Convert string to DateTime object
- `DateHelper::formatOrNull($dateTime, $format = 'Y-m-d H:i:s', $null = null)`: Format datetime or return null if invalid
- `DateHelper::diffHours($dateTime1, $dateTime2)`: Calculate hour differences between datetimes
- `DateHelper::diffDays($dateTime1, $dateTime2)`: Calculate day differences between datetimes
- `DateHelper::diffMonths($dateTime1, $dateTime2)`: Calculate month differences between datetimes
- `DateHelper::diffYears($dateTime1, $dateTime2)`: Calculate year differences between datetimes

### HTML Helper (`HtmlHelper.php`)

[](#html-helper-htmlhelperphp)

HTML element generation and attribute handling utilities.

#### Key Functions:

[](#key-functions-3)

- `HtmlHelper::chars($value, $double_encode = true)`: Convert special characters to HTML entities
- `HtmlHelper::entities($value, $double_encode = true)`: Convert all applicable characters to HTML entities
- `HtmlHelper::div($content, $attributesHelper = null)`: Create div element
- `HtmlHelper::p($content, $attributesHelper = null)`: Create paragraph element
- `HtmlHelper::span($content, $attributesHelper = null)`: Create span element
- `HtmlHelper::h1($content, $attributesHelper = null)` through `h6()`: Create heading elements
- `HtmlHelper::a($url, $content, $attributesHelper = null)`: Create anchor element
- `HtmlHelper::image($src, $attributesHelper = null)`: Create image element
- `HtmlHelper::autoParagraph($str, $br = true)`: Convert text to paragraphs

##### Text Formatting Elements:

[](#text-formatting-elements)

- `HtmlHelper::strong($content, $attributesHelper = null)`: Create strong (bold) element
- `HtmlHelper::em($content, $attributesHelper = null)`: Create emphasis (italic) element
- `HtmlHelper::code($content, $attributesHelper = null)`: Create code element
- `HtmlHelper::pre($content, $attributesHelper = null)`: Create preformatted text element
- `HtmlHelper::mark($content, $attributesHelper = null)`: Create highlighted text element
- `HtmlHelper::small($content, $attributesHelper = null)`: Create small text element
- `HtmlHelper::sub($content, $attributesHelper = null)`: Create subscript element
- `HtmlHelper::sup($content, $attributesHelper = null)`: Create superscript element

##### Semantic Elements:

[](#semantic-elements)

- `HtmlHelper::blockquote($content, $attributesHelper = null)`: Create blockquote element
- `HtmlHelper::cite($content, $attributesHelper = null)`: Create citation element
- `HtmlHelper::time($content, $datetime = null, $attributesHelper = null)`: Create time element with optional datetime attribute
- `HtmlHelper::abbr($content, $title = null, $attributesHelper = null)`: Create abbreviation element with optional title

##### Structural Elements:

[](#structural-elements)

- `HtmlHelper::article($content, $attributesHelper = null)`: Create article element
- `HtmlHelper::section($content, $attributesHelper = null)`: Create section element
- `HtmlHelper::nav($content, $attributesHelper = null)`: Create navigation element
- `HtmlHelper::aside($content, $attributesHelper = null)`: Create aside element
- `HtmlHelper::header($content, $attributesHelper = null)`: Create header element
- `HtmlHelper::footer($content, $attributesHelper = null)`: Create footer element
- `HtmlHelper::main($content, $attributesHelper = null)`: Create main element

##### Media Elements:

[](#media-elements)

- `HtmlHelper::figure($content, $attributesHelper = null)`: Create figure element
- `HtmlHelper::figcaption($content, $attributesHelper = null)`: Create figure caption element

##### Interactive Elements:

[](#interactive-elements)

- `HtmlHelper::details($content, $open = false, $attributesHelper = null)`: Create details element with optional open state
- `HtmlHelper::summary($content, $attributesHelper = null)`: Create summary element for details
- `HtmlHelper::dialog($content, $open = false, $modal = false, $attributesHelper = null)`: Create dialog element with optional open and modal states

##### Progress &amp; Measurement Elements:

[](#progress--measurement-elements)

- `HtmlHelper::meter($content, $value, $min = null, $max = null, $low = null, $high = null, $optimum = null, $attributesHelper = null)`: Create meter element with value ranges
- `HtmlHelper::progress($content, $value = null, $max = null, $attributesHelper = null)`: Create progress element with optional value and max

### JSON Helper (`JsonHelper.php`)

[](#json-helper-jsonhelperphp)

JSON validation and manipulation utilities.

#### Key Functions:

[](#key-functions-4)

- `JsonHelper::isValid($value)`: Validate various data types and JSON strings
- Support for complex JSON structures and nested objects

### Number Helper (`NumberHelper.php`)

[](#number-helper-numberhelperphp)

Number formatting and manipulation utilities.

#### Key Functions:

[](#key-functions-5)

- `NumberHelper::ordinal($number)`: Convert number to ordinal suffix (1st, 2nd, 3rd, etc.)

### Random Helper (`RandomHelper.php`)

[](#random-helper-randomhelperphp)

Random value generation utilities.

#### Key Functions:

[](#key-functions-6)

- `RandomHelper::trueFalse()`: Generate random boolean value
- `RandomHelper::uniqid($prefix = '')`: Generate unique identifier with optional prefix

### Value Helper (`ValueHelper.php`)

[](#value-helper-valuehelperphp)

Value validation and type checking utilities.

#### Key Functions:

[](#key-functions-7)

- `ValueHelper::emptyToNull(&$string)`: Convert empty value to null
- `ValueHelper::isEmpty($value)`: Check if value is empty (works with function returns)
- `ValueHelper::isInteger($value)`: Check if value is integer or string containing only digits
- `ValueHelper::isFloat($value)`: Check if value is float or string containing valid float format
- `ValueHelper::isDateTime($date, $format = null)`: Check if value is valid datetime
- `ValueHelper::isBool($value)`: Check if value is boolean type
- `ValueHelper::isTrue($value)`: Check if value is strictly TRUE
- `ValueHelper::isFalse($value)`: Check if value is strictly FALSE
- `ValueHelper::isTrueLike($value)`: Check if value evaluates to true in boolean context
- `ValueHelper::isFalseLike($value)`: Check if value evaluates to false in boolean context

### Counter Helper (`CounterHelper.php`)

[](#counter-helper-counterhelperphp)

Counter implementation for tracking and incrementing values.

#### Key Functions:

[](#key-functions-8)

- `CounterHelper::f($initialValue = 0)`: Create counter instance
- `CounterHelper::plusOne()`: Increment counter
- `CounterHelper::minusOne()`: Decrement counter
- `CounterHelper::getCurrentValue()`: Get current value
- `CounterHelper::getInitialValue()`: Get initial value
- `CounterHelper::getDifference()`: Calculate difference from initial value

### CSV Helper (`CsvHelper.php`)

[](#csv-helper-csvhelperphp)

CSV file handling and manipulation utilities.

#### Key Functions:

[](#key-functions-9)

- `CsvHelper::fileToArray($file, $hasTitleRow = false)`: Parse CSV file to array
- `CsvHelper::arrayToCsvString($array, $delimiter = ',', $enclosure = '"')`: Convert array to CSV string

### Email Helper (`EmailHelper.php`)

[](#email-helper-emailhelperphp)

Email validation and formatting utilities.

#### Key Functions:

[](#key-functions-10)

- `EmailHelper::clean($emails, $delimiters = [',', ';'])`: Clean and normalize email addresses
- `EmailHelper::isValid($email)`: Validate email address format

### IO Helper (`IOHelper.php`)

[](#io-helper-iohelperphp)

File system operations and temporary file handling.

#### Key Functions:

[](#key-functions-11)

- `IOHelper::createTmpDir($dir = null, $prefix = null, $absolute = false)`: Create temporary directory
- `IOHelper::createTmpFile($dir = null, $prefix = null, $absolute = false)`: Create temporary file
- `IOHelper::rmdirRecursive($dir)`: Recursively remove directory

### Timer Helper (`TimerHelper.php`)

[](#timer-helper-timerhelperphp)

Time measurement and execution timing utilities.

#### Key Functions:

[](#key-functions-12)

- `TimerHelper::start($alias)`: Start a new timer with specified alias
- `TimerHelper::getDifference($alias)`: Get elapsed time for a running timer
- `TimerHelper::stop($alias)`: Stop a timer and return elapsed time

### Color Helper (`Color/HexHelper.php`)

[](#color-helper-colorhexhelperphp)

Color manipulation and conversion utilities.

#### Key Functions:

[](#key-functions-13)

- `HexHelper::adjustBrightness($hex, $steps)`: Adjust brightness of a hex color code (-255 to 255)

### HTTP Helpers

[](#http-helpers)

#### Request Helper (`Http/RequestHelper.php`)

[](#request-helper-httprequesthelperphp)

Request environment detection and comprehensive HTTP request analysis utilities.

#### Key Functions:

[](#key-functions-14)

##### Environment Detection:

[](#environment-detection)

- `RequestHelper::isCli()`: Check if script is running in CLI mode
- `RequestHelper::isHttps()`: Check if request is using HTTPS (enhanced with proxy detection)
- `RequestHelper::isSecure()`: Check if request is secure (HTTPS or localhost)
- `RequestHelper::isLocalhost()`: Check if request is from localhost

##### HTTP Method Analysis:

[](#http-method-analysis)

- `RequestHelper::getMethod()`: Get the HTTP request method (GET, POST, etc.)
- `RequestHelper::isMethod($method)`: Check if request method matches given method
- `RequestHelper::isGet()`: Check if request is GET
- `RequestHelper::isPost()`: Check if request is POST
- `RequestHelper::isPut()`: Check if request is PUT
- `RequestHelper::isDelete()`: Check if request is DELETE
- `RequestHelper::isHead()`: Check if request is HEAD
- `RequestHelper::isOptions()`: Check if request is OPTIONS
- `RequestHelper::isPatch()`: Check if request is PATCH

##### Request Type Detection:

[](#request-type-detection)

- `RequestHelper::isAjax()`: Check if request is AJAX/XMLHttpRequest
- `RequestHelper::isApi()`: Check if request is likely an API request
- `RequestHelper::isMobile()`: Check if request is from mobile device
- `RequestHelper::isBot()`: Check if request is from bot/crawler

##### Client Information:

[](#client-information)

- `RequestHelper::getClientIp($trustProxies = true)`: Get client IP address with proxy support
- `RequestHelper::getUserAgent()`: Get user agent string
- `RequestHelper::getReferrer()`: Get referrer URL
- `RequestHelper::getProtocol()`: Get request protocol (HTTP/1.1, HTTP/2, etc.)
- `RequestHelper::getPort()`: Get request port
- `RequestHelper::getHost()`: Get host name

##### Content Analysis:

[](#content-analysis)

- `RequestHelper::getContentType()`: Get request content type
- `RequestHelper::isContentType($type)`: Check if content type matches
- `RequestHelper::isJson()`: Check if request has JSON content type
- `RequestHelper::isXml()`: Check if request has XML content type
- `RequestHelper::getContentLength()`: Get content length
- `RequestHelper::getAcceptedLanguages()`: Get accepted languages from Accept-Language header

##### Header Management:

[](#header-management)

- `RequestHelper::getHeader($name, $default = null)`: Get specific request header
- `RequestHelper::hasHeader($name)`: Check if header exists
- `RequestHelper::getAllHeaders()`: Get all request headers

##### Security and Validation:

[](#security-and-validation)

- `RequestHelper::isLegitimate()`: Basic security check for legitimate requests
- `RequestHelper::getRequestTime()`: Get request timestamp
- `RequestHelper::isWithinRateLimit($maxRequests, $timeWindow, $identifier = null)`: Basic rate limiting

##### Utility Methods:

[](#utility-methods)

- `RequestHelper::getRequestInfo()`: Get comprehensive request information array
- `RequestHelper::setTrustedProxyHeaders($headers)`: Set trusted proxy headers
- `RequestHelper::getTrustedProxyHeaders()`: Get current trusted proxy headers

#### URL Helper (`Http/UrlHelper.php`)

[](#url-helper-httpurlhelperphp)

Comprehensive URL manipulation, validation, and generation utilities for HTTP requests.

##### Current URL/URI Methods:

[](#current-urluri-methods)

- `UrlHelper::protocolHostPort()`: Get protocol, host, and port string based on server configuration
- `UrlHelper::query($parameters = null, $mergeGetVariables = true)`: Build query string from parameters with optional $\_GET merging
- `UrlHelper::currentUrl($includeQuery = true, $urlEncode = false)`: Get current full URL with optional query string and encoding
- `UrlHelper::currentUri($includeQueryParams = true, $encode = false)`: Get current URI with optional query parameters and encoding

##### URL Parsing and Validation:

[](#url-parsing-and-validation)

- `UrlHelper::isValidUrl($url, $allowedSchemes = ['http', 'https'])`: Validate URL with configurable allowed schemes
- `UrlHelper::parseUrl($url)`: Parse URL components into structured array
- `UrlHelper::buildUrl($components)`: Build URL from component array
- `UrlHelper::isStandardPort($scheme, $port)`: Check if port is standard for given scheme

##### URL Manipulation:

[](#url-manipulation)

- `UrlHelper::addQueryParams($url, $params, $encode = true)`: Add or modify query parameters in URL
- `UrlHelper::removeQueryParams($url, $paramsToRemove)`: Remove specific query parameters from URL
- `UrlHelper::changeScheme($url, $scheme)`: Change URL scheme (e.g., HTTP to HTTPS)
- `UrlHelper::normalize($url)`: Normalize URL by cleaning up common issues

##### Path Manipulation:

[](#path-manipulation)

- `UrlHelper::normalizePath($path)`: Normalize URL path by resolving . and .. segments
- `UrlHelper::joinPaths(...$segments)`: Join multiple path segments into single path
- `UrlHelper::getDirectory($path)`: Get directory path from URL path
- `UrlHelper::getFilename($path)`: Get filename from URL path
- `UrlHelper::getExtension($path)`: Get file extension from URL path

##### Domain and Host Utilities:

[](#domain-and-host-utilities)

- `UrlHelper::getDomain($url)`: Extract domain from URL
- `UrlHelper::getSubdomain($url, $levels = 2)`: Extract subdomain with configurable root domain levels
- `UrlHelper::getRootDomain($url, $levels = 2)`: Get root domain with configurable levels
- `UrlHelper::isSameDomain($url1, $url2)`: Check if two URLs have same domain

##### Encoding and Decoding:

[](#encoding-and-decoding)

- `UrlHelper::encode($string)`: URL encode string with RFC 3986 compliance
- `UrlHelper::decode($string)`: URL decode string
- `UrlHelper::encodePath($path)`: Encode only path component of URL
- `UrlHelper::encodeQuery($params, $rfc3986 = true)`: Encode query string parameters

##### URL Conversion and Transformation:

[](#url-conversion-and-transformation)

- `UrlHelper::toAbsolute($relativeUrl, $baseUrl)`: Convert relative URL to absolute
- `UrlHelper::toRelative($absoluteUrl, $baseUrl)`: Convert absolute URL to relative

##### Utility Methods:

[](#utility-methods-1)

- `UrlHelper::currentUrlWithModifications($queryModifications = [], $queryRemovals = [])`: Get current URL with query modifications
- `UrlHelper::isSecureUrl($url)`: Check if URL uses HTTPS scheme
- `UrlHelper::getStandardPort($scheme)`: Get standard port for scheme
- `UrlHelper::sanitize($url, $allowedSchemes = ['http', 'https'])`: Sanitize URL by removing dangerous protocols
- `UrlHelper::modifiedQuery($params = [], $remove = [])`: Generate query string from current URL with modifications

##### Constants:

[](#constants)

- URL schemes: `SCHEME_HTTP`, `SCHEME_HTTPS`, `SCHEME_FTP`, `SCHEME_SFTP`, `SCHEME_FILE`
- Standard ports array: `STANDARD_PORTS` with common protocol ports

### HTML Helpers

[](#html-helpers)

#### Form Helper (`Html/FormHelper.php`)

[](#form-helper-htmlformhelperphp)

HTML form element generation utilities.

#### Key Functions:

[](#key-functions-15)

- `FormHelper::open($action = null, $method = 'GET', $attributesHelper = null)`: Create form opening tag
- `FormHelper::close()`: Create form closing tag
- `FormHelper::text($name, $value = null, $attributesHelper = null)`: Create text input
- `FormHelper::password($name, $value = null, $attributesHelper = null)`: Create password input
- `FormHelper::email($name, $value = null, $attributesHelper = null)`: Create email input
- `FormHelper::color($name, $value = null, $attributesHelper = null)`: Create color input
- `FormHelper::date($name, $value = null, $attributesHelper = null)`: Create date input
- `FormHelper::datetime($name, $value = null, $attributesHelper = null)`: Create datetime input
- `FormHelper::datetimeLocal($name, $value = null, $attributesHelper = null)`: Create datetime-local input
- `FormHelper::month($name, $value = null, $attributesHelper = null)`: Create month input
- `FormHelper::number($name, $value = null, $attributesHelper = null)`: Create number input
- `FormHelper::range($name, $value, $min, $max, $attributesHelper = null)`: Create range input
- `FormHelper::search($name, $value = null, $attributesHelper = null)`: Create search input
- `FormHelper::tel($name, $value = null, $attributesHelper = null)`: Create telephone input
- `FormHelper::time($name, $value = null, $attributesHelper = null)`: Create time input
- `FormHelper::url($name, $value = null, $attributesHelper = null)`: Create URL input
- `FormHelper::week($name, $value = null, $attributesHelper = null)`: Create week input
- `FormHelper::hidden($name, $value = null, $attributesHelper = null)`: Create hidden input
- `FormHelper::textarea($name, $value = null, $attributesHelper = null)`: Create textarea
- `FormHelper::button($name, $value = null, $attributesHelper = null)`: Create button
- `FormHelper::submit($name, $value = null, $attributesHelper = null)`: Create submit button
- `FormHelper::label($value, $forId = null, $formId = null, $attributesHelper = null)`: Create label
- `FormHelper::select($name, array $options, $checkedValue = null, $attributesHelper = null)`: Create select
- `FormHelper::selectMultiple($name, array $options, $checkedValue = null, $attributesHelper = null)`: Create multiple select
- `FormHelper::option($value, $text, $selected = false)`: Create option
- `FormHelper::optgroup($label, $htmlContent, $attributesHelper = null)`: Create option group
- `FormHelper::checkbox($name, $value = null, $checked = false, $attributesHelper = null)`: Create checkbox
- `FormHelper::radio($name, $value = null, $checked = false, $attributesHelper = null)`: Create radio button

#### Attributes Helper (`Html/AttributesHelper.php`)

[](#attributes-helper-htmlattributeshelperphp)

A powerful utility class for managing HTML attributes with proper escaping and validation.

```
// Create a new instance with initial attributes
$attrs = AttributesHelper::f(['class' => 'btn']);

// Add more classes and attributes
$attrs->addClass('btn-primary')
     ->addStyle('margin', '10px')
     ->addData('toggle', 'modal');

// Render as HTML attributes string
echo $attrs; // outputs: class="btn btn-primary" style="margin:10px" data-toggle="modal"
```

##### Key Features:

[](#key-features)

- Type-safe attribute handling
- HTML escaping for security
- CSS style parsing and validation
- Data attribute management
- Class name management

##### Main Methods:

[](#main-methods)

- `AttributesHelper::f($input = null)`: Create new instance (recommended factory method)
- `setAttribute(string $name, mixed $value)`: Set any attribute with validation
- `setId(string $value)`: Set the ID attribute
- `addClass(string|array $classes)`: Add one or more CSS classes
- `addData(string $name, mixed $value)`: Add a data attribute
- `addStyle(string $property, string $value)`: Add a CSS style property
- `render()`: Convert to HTML attributes string
- `toArray()`: Get all attributes as an array

#### Bootstrap Helper (`Html/BootstrapHelper.php`)

[](#bootstrap-helper-htmlbootstraphelperphp)

Bootstrap-specific HTML generation utilities.

#### Key Functions:

[](#key-functions-16)

- `BootstrapHelper::glyphIcon($name, $attributesHelper = null)`: Create Bootstrap glyphicon span element

```
use AndreasGlaser\Helpers\Html\BootstrapHelper;

// Basic glyphicon
echo BootstrapHelper::glyphIcon('home');
// Output:

// With additional attributes
echo BootstrapHelper::glyphIcon('search', [
    'id' => 'search-icon',
    'class' => 'icon-large text-primary',
    'title' => 'Search',
    'data-toggle' => 'tooltip'
]);
// Output:

// Using AttributesHelper
$attrs = AttributesHelper::f()
    ->setId('my-icon')
    ->addClass('text-danger')
    ->addData('action', 'delete');
echo BootstrapHelper::glyphIcon('trash', $attrs);
```

#### Table Helper (`Html/Table/TableHelper.php`)

[](#table-helper-htmltabletablehelperphp)

HTML table generation utilities.

#### Key Functions:

[](#key-functions-17)

- `TableHelper::f($headRows = null, $bodyRows = null, $attributesHelper = null)`: Create table instance
- `TableHelper::addHeadRow($rowHelper, $setCellAsHeaders = true)`: Add header row
- `TableHelper::addBodyRow($rowHelper)`: Add body row
- `TableHelper::render($renderer = null)`: Render table as HTML

#### List Helpers

[](#list-helpers)

##### Base List Helper (`Html/Lists/BaseListHelper.php`)

[](#base-list-helper-htmllistsbaselisthelperphp)

Base class for HTML list generation.

#### Key Functions:

[](#key-functions-18)

- `BaseListHelper::f($items = null, $attributesHelper = null)`: Create list instance
- `BaseListHelper::addItem($content, $attributesHelper = null)`: Add item to list
- `BaseListHelper::getItems()`: Get all list items
- `BaseListHelper::getAttributes()`: Get list attributes

##### Unordered List Helper (`Html/Lists/UnorderedListHelper.php`)

[](#unordered-list-helper-htmllistsunorderedlisthelperphp)

HTML unordered list generation utilities.

#### Key Functions:

[](#key-functions-19)

- `UnorderedListHelper::render($renderer = null)`: Render unordered list as HTML

##### Ordered List Helper (`Html/Lists/OrderedListHelper.php`)

[](#ordered-list-helper-htmllistsorderedlisthelperphp)

HTML ordered list generation utilities.

#### Key Functions:

[](#key-functions-20)

- `OrderedListHelper::render($renderer = null)`: Render ordered list as HTML

##### Description List Helper (`Html/Lists/DescriptionListHelper.php`)

[](#description-list-helper-htmllistsdescriptionlisthelperphp)

HTML description list generation utilities.

#### Key Functions:

[](#key-functions-21)

- `DescriptionListHelper::addTerm($term, $description, $attributesHelper = null)`: Add term and description
- `DescriptionListHelper::render($renderer = null)`: Render description list as HTML

### View Helper (`View/PHPView.php`)

[](#view-helper-viewphpviewphp)

PHP template rendering and view management utilities.

#### Key Functions:

[](#key-functions-22)

- `PHPView::f($file = null, $data = [])`: Create view instance
- `PHPView::setGlobal($key, $value)`: Set global data accessible to all views
- `PHPView::getGlobalData()`: Get all global data
- `PHPView::setFile($filePath)`: Set template file path
- `PHPView::set($key, $value)`: Set local data for view instance
- `PHPView::getData()`: Get all local data
- `PHPView::render($file = null)`: Render template with current data
- `PHPView::__toString()`: Render template when used as string

### Network Helper (`Validate/NetworkHelper.php`)

[](#network-helper-validatenetworkhelperphp)

Network-related validation utilities.

#### Key Functions:

[](#key-functions-23)

- `NetworkHelper::isValidIPv4($ip)`: Validate IPv4 address format
- `NetworkHelper::isValidIPv6($ip)`: Validate IPv6 address format
- `NetworkHelper::isValidIP($ip, $allowPrivate = true, $allowReserved = true)`: Validate IP address (both IPv4 and IPv6) with support for private and reserved range validation
- `NetworkHelper::isValidPort($port, $allowSystemPorts = true, $allowUserPorts = true, $allowDynamicPorts = true)`: Validate port number with customizable range restrictions
- `NetworkHelper::isValidDomain($domain, $allowSingleLabel = false, $allowPunycode = true)`: Validate domain name with support for Punycode and single-label domains
- `NetworkHelper::isValidMac($mac, $allowColonFormat = true, $allowHyphenFormat = true, $allowBareFormat = true)`: Validate MAC address in various formats
- `NetworkHelper::isValidCidr($cidr)`: Validate CIDR notation for both IPv4 and IPv6
- `NetworkHelper::isValidSubnetMask($mask)`: Validate IPv4 subnet mask
- `NetworkHelper::getCommonPort($service)`: Get common port number for well-known services

##### DNS Operations:

[](#dns-operations)

- `NetworkHelper::getDnsRecords($domain, $type = 'ALL')`: Get DNS records for a domain with support for all record types (A, AAAA, MX, NS, TXT, etc.)
- `NetworkHelper::isValidMxRecord($domain)`: Check if a domain has valid MX records
- `NetworkHelper::getReverseDns($ip)`: Get reverse DNS (PTR) record for an IP address
- `NetworkHelper::hasValidSpfRecord($domain)`: Check if a domain has a valid SPF record
- `NetworkHelper::hasDkimRecord($domain, $selector)`: Check if a domain has a valid DKIM record for a selector

##### Socket and Port Operations:

[](#socket-and-port-operations)

- `NetworkHelper::isPortOpen($host, $port, $timeout = 2.0)`: Check if a specific port is open on a host
- `NetworkHelper::getOpenPorts($host, array $ports, $timeout = 1.0)`: Scan multiple ports on a host
- `NetworkHelper::getServiceByPort($port)`: Get service name for a port number (e.g., 80 → "http")

### Validation Helpers

[](#validation-helpers)

#### Expect Helper (`Validate/Expect.php`)

[](#expect-helper-validateexpectphp)

Type validation utilities that throw exceptions on type mismatches. All methods throw `UnexpectedTypeException` if the value doesn't match the expected type.

##### Basic Type Validation:

[](#basic-type-validation)

- `Expect::int($value)`: Validates that a value is an integer
- `Expect::float($value)`: Validates that a value is a float
- `Expect::str($value)`: Validates that a value is a string
- `Expect::bool($value)`: Validates that a value is a boolean
- `Expect::arr($value)`: Validates that a value is an array
- `Expect::obj($value)`: Validates that a value is an object
- `Expect::res($value)`: Validates that a value is a resource
- `Expect::null($value)`: Validates that a value is null

##### Special Type Validation:

[](#special-type-validation)

- `Expect::numeric($value)`: Validates that a value is numeric (int, float, or numeric string)
- `Expect::isCallable($value)`: Validates that a value is callable (function, closure, method array, etc.)
- `Expect::scalar($value)`: Validates that a value is scalar (int, float, string, or bool)

##### Built-in PHP Type Validation:

[](#built-in-php-type-validation)

- `Expect::countable($value)`: Validates that a value is countable (array or implements Countable)
- `Expect::iterable($value)`: Validates that a value is iterable (array or implements Traversable)
- `Expect::finite($value)`: Validates that a value is a finite number (not infinite or NaN)
- `Expect::infinite($value)`: Validates that a value is an infinite number
- `Expect::nan($value)`: Validates that a value is NaN (Not a Number)

#### IOExpect Helper (`Validate/IOExpect.php`)

[](#ioexpect-helper-validateioexpectphp)

File system validation utilities that throw exceptions on validation failures. All methods throw `IOException` if the validation fails.

##### Path Existence and Type Validation:

[](#path-existence-and-type-validation)

- `IOExpect::exists($path)`: Validates that a path exists (file or directory)
- `IOExpect::doesNotExist($path)`: Validates that a path does not exist
- `IOExpect::isDir($path)`: Validates that a path exists and is a directory
- `IOExpect::isFile($path)`: Validates that a path exists and is a file
- `IOExpect::isLink($path)`: Validates that a path is a symbolic link

##### Permission Validation:

[](#permission-validation)

- `IOExpect::isReadable($path)`: Validates that a path is readable
- `IOExpect::isWritable($path)`: Validates that a path is writable
- `IOExpect::isExecutable($path)`: Validates that a path is executable
- `IOExpect::parentDirWritable($path)`: Validates that parent directory exists and is writable

##### Directory Content Validation:

[](#directory-content-validation)

- `IOExpect::isDirEmpty($path)`: Validates that a directory is empty
- `IOExpect::isDirNotEmpty($path)`: Validates that a directory is not empty

##### File Content and Properties Validation:

[](#file-content-and-properties-validation)

- `IOExpect::isFileNotEmpty($path)`: Validates that a file is not empty
- `IOExpect::hasMinSize($path, $minSize)`: Validates that a file has minimum size in bytes
- `IOExpect::hasMaxSize($path, $maxSize)`: Validates that a file has maximum size in bytes
- `IOExpect::hasExtension($path, $extension)`: Validates that a file has specific extension
- `IOExpect::hasAllowedExtension($path, $extensions)`: Validates that a file has one of allowed extensions
- `IOExpect::hasMimeType($path, $expectedMimeType)`: Validates that a file matches specific MIME type

Testing
-------

[](#testing)

The library includes comprehensive unit tests for all components. Each helper class has a corresponding test class that verifies its functionality:

- `ArrayHelperTest`: Tests array manipulation and path operations
- `AttributesHelperTest`: Tests HTML attribute management and validation
- `BootstrapHelperTest`: Tests Bootstrap component generation and glyphicon creation
- `CounterHelperTest`: Tests counter operations and assertions
- `CsvHelperTest`: Tests CSV file operations and string conversion
- `DateHelperTest`: Tests date formatting and difference calculations
- `EmailHelperTest`: Tests email validation and cleaning
- `ExpectTest`: Tests type validation and exception throwing
- `FormHelperTest`: Tests HTML form element generation and validation
- `HtmlHelperTest`: Tests HTML element generation and attributes
- `IOExpectTest`: Tests file system validation and IOException handling
- `IOHelperTest`: Tests file system operations
- `JsonHelperTest`: Tests JSON validation for various data types
- `NumberHelperTest`: Tests number formatting and ordinal conversion
- `RandomHelperTest`: Tests random value generation
- `RequestHelperTest`: Tests comprehensive HTTP request analysis including environment detection, method analysis, client information, content analysis, and security features
- `StringHelperTest`: Tests string comparison and manipulation methods
- `UrlHelperTest`: Tests comprehensive URL manipulation including validation, parsing, path operations, domain extraction, encoding, and utility methods
- `ValueHelperTest`: Tests value validation and type checking

Run the tests using:

```
composer install
./vendor/phpunit/phpunit/phpunit
```

Usage
-----

[](#usage)

```
use AndreasGlaser\Helpers\ArrayHelper;
use AndreasGlaser\Helpers\StringHelper;
use AndreasGlaser\Helpers\DateHelper;
use AndreasGlaser\Helpers\ValueHelper;
use AndreasGlaser\Helpers\CsvHelper;
use AndreasGlaser\Helpers\EmailHelper;
use AndreasGlaser\Helpers\Html\FormHelper;
use AndreasGlaser\Helpers\Html\AttributesHelper;
use AndreasGlaser\Helpers\Validate\Expect;
use AndreasGlaser\Helpers\Validate\IOExpect;

// Array operations
$array = ['user' => ['profile' => ['name' => 'John']]];
$name = ArrayHelper::getByPath($array, 'user.profile.name'); // Returns 'John'

// String operations
$string = 'Hello World';
$contains = StringHelper::contains($string, 'World'); // Returns true
$startsWith = StringHelper::startsWith($string, 'Hello'); // Returns true

// Date operations
$date = new DateTime();
$hours = DateHelper::diffHours($date, new DateTime('+1 day')); // Returns 24

// Value validation
$isValid = ValueHelper::isDateTime('2024-03-20'); // Returns true

// CSV operations
$csvData = [
    ['Name', 'Email', 'Age'],
    ['John Doe', 'john@example.com', '30'],
    ['Jane Smith', 'jane@example.com', '25']
];
$csvString = CsvHelper::arrayToCsvString($csvData);
// Result: "Name,Email,Age\nJohn Doe,john@example.com,30\nJane Smith,jane@example.com,25"

// Read CSV file to array
$data = CsvHelper::fileToArray('users.csv', true); // true for header row
// With custom delimiter
$data = CsvHelper::fileToArray('data.csv', false, 0, ';'); // semicolon delimiter

// Email operations
$validEmail = EmailHelper::isValid('user@example.com'); // Returns true
$invalidEmail = EmailHelper::isValid('invalid-email'); // Returns false

// Clean and normalize email addresses
$emails = 'user@example.com, invalid-email, admin@test.com; contact@domain.org';
$cleanEmails = EmailHelper::clean($emails); // Returns ['user@example.com', 'admin@test.com', 'contact@domain.org']

// Clean with custom delimiters
$emails = 'user@example.com:admin@test.com#contact@domain.org';
$cleanEmails = EmailHelper::clean($emails, [':', '#']); // Returns ['user@example.com', 'admin@test.com', 'contact@domain.org']

// Clean array of emails
$emailArray = ['user@example.com', 'invalid', 'admin@test.com'];
$cleanEmails = EmailHelper::clean($emailArray); // Returns ['user@example.com', 'admin@test.com']

// Form generation
echo FormHelper::open('/users', 'POST', ['class' => 'user-form']);
echo FormHelper::text('name', 'John Doe', ['id' => 'name', 'required' => 'required']);
echo FormHelper::email('email', 'john@example.com', ['placeholder' => 'Enter email']);
echo FormHelper::select('country', ['US' => 'United States', 'CA' => 'Canada'], 'US');
echo FormHelper::textarea('bio', 'Tell us about yourself', ['rows' => 5]);
echo FormHelper::submit('submit', 'Create User', ['class' => 'btn btn-primary']);
echo FormHelper::close();

// HTML attribute management
$attrs = AttributesHelper::f(['class' => 'btn'])
    ->addClass('btn-primary')
    ->addStyle('margin', '10px')
    ->addData('toggle', 'modal');
echo $attrs; // outputs: class="btn btn-primary" style="margin:10px" data-toggle="modal"

// Type validation with exceptions
Expect::int(42);           // Valid - no exception
Expect::str('hello');      // Valid - no exception
Expect::finite(42.5);      // Valid - finite number

// File system validation
IOExpect::isFile('/path/to/file.txt');    // Throws IOException if not a file
IOExpect::isReadable('/path/to/file.txt'); // Throws IOException if not readable
```

Contributing
------------

[](#contributing)

1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

License
-------

[](#license)

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

Author
------

[](#author)

- **Andreas Glaser** - [GitHub](https://github.com/andreas-glaser)

Support
-------

[](#support)

If you find this library helpful, please consider giving it a star on GitHub!

###  Health Score

53

—

FairBetter than 97% of packages

Maintenance53

Moderate activity, may be stable

Popularity37

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity89

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 99.5% 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 ~122 days

Recently: every ~497 days

Total

29

Last Release

354d ago

Major Versions

v1.3.0 → v2.0.02025-05-29

PHP version history (5 changes)1.0.0PHP &gt;=5.4.0

1.1.0PHP ^7

1.0.11PHP ^5.4

v1.2.0PHP ^8.2|^8.3|^8.4

v2.0.0PHP ^8.2|^8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/efe09312b81341c87f6f49c29c6d67e8b6045e598165c339f126afbbc173dd42?d=identicon)[andreas-glaser](/maintainers/andreas-glaser)

---

Top Contributors

[![andreas-glaser](https://avatars.githubusercontent.com/u/2282942?v=4)](https://github.com/andreas-glaser "andreas-glaser (185 commits)")[![dontub](https://avatars.githubusercontent.com/u/5405481?v=4)](https://github.com/dontub "dontub (1 commits)")

---

Tags

helpersphputilitiesvalidationphpvalidationhelpersstring manipulationutilitiesArray manipulationphp8type-safemodern-phpform-builderdate-timedata-manipulationhtml-generationdevelopment-toolsutility-functions

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/andreas-glaser-php-helpers/health.svg)

```
[![Health](https://phpackages.com/badges/andreas-glaser-php-helpers/health.svg)](https://phpackages.com/packages/andreas-glaser-php-helpers)
```

###  Alternatives

[event4u/data-helpers

Framework-agnostic PHP library for data mapping, DTOs and utilities. Includes DataMapper, SimpleDto/LiteDto, DataAccessor/Mutator/Filter and helper classes (MathHelper, EnvHelper, etc.). Works with Laravel, Symfony/Doctrine or standalone PHP.

1421.5k](/packages/event4u-data-helpers)[transprime-research/piper

PHP Pipe method execution with values from chained method executions

174.6k2](/packages/transprime-research-piper)

PHPackages © 2026

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