The library clausnz/php-helpers
is a collection of 45 useful php helper functions (PHP 5.6, 7.*)
.
After installation with composer
, the global functions are accessable from everywhere in your code:
composer require clausnz/php-helpers
<?php
dump( 'any content' );
If a function with the same name already exists in the list of your project's defined functions ( built-in and user-defined ), it will simply not be registered in your environment. Therefore, no conflicts with existing functions will appear.
Nevertheless, every function is still accessable it in a static way with the proper use-statement:
<?php
use CNZHelpersUtil as util;
util::dump( 'any content' );
This library makes use of the following brilliant and well known libraries:
All functions are tested against a number of unit tests and PHP Versions.
Install the latest clausnz/php-helper
library with composer:
composer require clausnz/php-helpers
Also make sure to require your composer autoload file:
require __DIR__ . '/vendor/autoload.php';
After installation, the new global PHP functions are available everywhere in your code. To access the ( almost identical ) static functions in the helper classes, add the proper use statement to your file:
<?php
use CNZHelpersDev as dev;
if( dev::isIphone() ) {
// Do something here
}
Helper class that provides easy access to useful php array functions.
Class Arr
Detects if the given value is an associative array.
Arr::isAssoc( array $array ): boolean
Related global function (description see above).
( jump back )
is_assoc( array $array ): boolean
$array = [
'foo' => 'bar'
];
is_assoc( $array );
// bool(true)
Parameter | Type | Description |
---|---|---|
$array |
array | Any type of array. |
Return Value:
True if the array is associative, false otherwise.
Converts an array to an object.
Arr::toObject( array $array ): object|null
Related global function (description see above).
( jump back )
to_object( array $array ): object|null
$array = [
'foo' => [
'bar' => 'baz'
]
];
$obj = to_object($array);
echo $obj->foo->bar;
// baz
Parameter | Type | Description |
---|---|---|
$array |
array | The array to be converted. |
Return Value:
A std object representation of the converted array.
Converts a string or an object to an array.
Arr::dump( string|object $var ): array|null
Related global function (description see above).
( jump back )
to_array( string|object $var ): array|null
$var = 'php';
to_array( $var );
// (
// [0] => p
// [1] => h
// [2] => p
// )
$var = new stdClass;
$var->foo = 'bar';
to_array( $var );
// (
// [foo] => bar
// )
Parameter | Type | Description |
---|---|---|
$var |
string|object | String or object. |
Return Value:
An array representation of the converted string or object. Returns null on error.
Returns the first element of an array.
Arr::first( array $array ): mixed
Related global function (description see above).
( jump back )
array_first( array $array ): mixed
$array = [
'foo' => 'bar',
'baz' => 'qux'
];
array_first( $array )
// bar
Parameter | Type | Description |
---|---|---|
$array |
array | The concerned array. |
Return Value:
The value of the first element, without key. Mixed type.
Returns the last element of an array.
Arr::last( array $array ): mixed
Related global function (description see above).
( jump back )
array_last( array $array ): mixed
$array = [
'foo' => 'bar',
'baz' => 'qux'
];
array_last( $array )
// qux
Parameter | Type | Description |
---|---|---|
$array |
array | The concerned array. |
Return Value:
The value of the last element, without key. Mixed type.
Gets a value in an array by dot notation for the keys.
Arr::get( string $key, array $array ): mixed
Related global function (description see above).
( jump back )
array_get( string key, array $array ): mixed
$array = [
'foo' => 'bar',
'baz' => [
'qux => 'foobar'
]
];
array_get( 'baz.qux', $array );
// foobar
Parameter | Type | Description |
---|---|---|
$key |
string | The key by dot notation. |
$array |
array | The array to search in. |
Return Value:
The searched value, null otherwise.
Sets a value in an array using the dot notation.
Arr::set( string $key, mixed $value, array &$array ): boolean
Related global function (description see above).
( jump back )
array_set( string key, mixed value, array $array ): boolean
$array = [
'foo' => 'bar',
'baz' => [
'qux => 'foobar'
]
];
array_set( 'baz.qux', 'bazqux', $array );
// (
// [foo] => bar
// [baz] => [
// [qux] => bazqux
// ]
// )
$array = [
'foo' => 'bar',
'baz' => [
'qux => 'foobar'
]
];
array_set( 'baz.foo', 'bar', $array );
// (
// [foo] => bar
// [baz] => [
// [qux] => bazqux
// [foo] => bar
// ]
// )
Parameter | Type | Description |
---|---|---|
$key |
string | The key to set using dot notation. |
$value |
mixed | The value to set on the specified key. |
$array |
array | The concerned array. |
Return Value:
True if the new value was successfully set, false otherwise.
Helper class that provides easy access to useful php functions in conjunction with the user agent.
Class Dev
Determes if the current device is a smartphone.
Dev::isSmartphone( ): boolean
Related global function (description see above).
( jump back )
is_smartphone( ): boolean
if ( is_smartphone() ) {
// I am a smartphone
}
Return Value:
True if current visitor uses a smartphone, false otherwise.
Detects if the current visitor uses a mobile device (Smartphone/Tablet/Handheld).
Dev::isMobile( ): boolean
Related global function (description see above).
( jump back )
is_mobile( ): boolean
if ( is_mobile() ) {
// I am a mobile device (smartphone/tablet or handheld)
}
Return Value:
True if current visitor uses a mobile device, false otherwise.
Get a singleton MobileDetect object to call every method it provides.
Dev::mobileDetect( ): DetectionMobileDetect
Public access for use of outside this class. Mobile_Detect doku: https://github.com/serbanghita/Mobile-Detect
This method has no related global function!
( jump back )
Dev::mobileDetect()->version('Android');
// 8.1
Return Value:
A singleton MobileDetect object to call every method it provides.
Determes if the current visitor uses a tablet device.
Dev::isTablet( ): boolean
Related global function (description see above).
( jump back )
is_tablet( ): boolean
if ( is_tablet() ) {
// I am a tablet
}
Return Value:
True if current visitor uses a tablet device, false otherwise.
Determes if the current visitor uses a desktop computer.
Dev::isDesktop( ): boolean
Related global function (description see above).
( jump back )
is_desktop( ): boolean
if ( is_desktop() ) {
// I am a desktop computer (Mac, Linux, Windows)
}
Return Value:
True if current visitor uses a desktop computer, false otherwise.
Determes if the current visitor is a search engine/bot/crawler/spider.
Dev::isRobot( ): boolean
Related global function (description see above).
( jump back )
is_robot( ): boolean
if ( is_robot() ) {
// I am a robot (search engine, bot, crawler, spider)
}
Return Value:
True if the current visitor is a search engine/bot/crawler/spider, false otherwise.
Get a singleton CrawlerDetect object to call every method it provides.
Dev::crawlerDetect( ): JaybizzleCrawlerDetectCrawlerDetect
Public access for use of outside this class. Crawler-Detect doku: https://github.com/JayBizzle/Crawler-Detect
This method has no related global function!
( jump back )
Dev::crawlerDetect()->getMatches();
// Output the name of the bot that matched (if any)
Determes if the current device is running an Android operating system.
Dev::isAndroid( ): boolean
Related global function (description see above).
( jump back )
is_android( ): boolean
if ( is_android() ) {
// I am an Android based device
}
Return Value:
True if current visitor uses an Android based device, false otherwise.
Determes if the current device is an iPhone.
Dev::isIphone( ): boolean
Related global function (description see above).
( jump back )
is_iphone( ): boolean
if ( is_iphone() ) {
// I am an iPhone
}
Return Value:
True if current visitor uses an iPhone, false otherwise.
Determes if the current device is from Samsung.
Dev::isSamsung( ): boolean
Related global function (description see above).
( jump back )
is_samsung( ): boolean
if ( is_samsung() ) {
// I am a device from Samsung
}
Return Value:
True if current visitor uses a Samsung device, false otherwise.
Determes if the current device is running an iOS operating system.
Dev::isIOS( ): boolean
Related global function (description see above).
( jump back )
is_ios( ): boolean
if ( is_ios() ) {
// I am an iOS based device
}
Return Value:
True if current visitor uses an iOS device, false otherwise.
Helper class that provides easy access to useful php string functions.
Class Str
Inserts one or more strings into another string on a defined position.
Str::insert( array $keyValue, string $string ): string
Related global function (description see above).
( jump back )
str_insert( array $keyValue, string $string ): string
$keyValue = [
':color' => 'brown',
':animal' => 'dog'
]
$string = 'The quick :color fox jumps over the lazy :animal.';
str_insert( $keyValue, $string );
// The quick brown fox jumps over the lazy dog.
Parameter | Type | Description |
---|---|---|
$keyValue |
array | An associative array with key => value pairs. |
$string |
string | The text with the strings to be replaced. |
Return Value:
The replaced string.
Return the content in a string between a left and right element.
Str::between( string $left, string $right, string $string ): array
Related global function (description see above).
( jump back )
str_between( string $left, string $right, string $string ): array
$string = '<tag>foo</tag>foobar<tag>bar</tag>'
str_between( '<tag>', '</tag>' $string );
// (
// [0] => foo
// [1] => bar
// )
Parameter | Type | Description |
---|---|---|
$left |
string | The left element of the string to search. |
$right |
string | The right element of the string to search. |
$string |
string | The string to search in. |
Return Value:
A result array with all matches of the search.
Return the part of a string after a given value.
Str::after( string $search, string $string ): string
Related global function (description see above).
( jump back )
str_after( string $search, string $string ): string
$string = 'The quick brown fox jumps over the lazy dog';
str_after( 'fox' $string );
// jumps over the lazy dog
Parameter | Type | Description |
---|---|---|
$search |
string | The string to search for. |
$string |
string | The string to search in. |
Return Value:
The found string after the search string. Whitespaces at beginning will be removed.
Get the part of a string before a given value.
Str::before( string $search, string $string ): string
Related global function (description see above).
( jump back )
str_before( string $search, string $string ): string
$string = 'The quick brown fox jumps over the lazy dog';
str_before( 'fox' $string );
// The quick brown
Parameter | Type | Description |
---|---|---|
$search |
string | The string to search for. |
$string |
string | The string to search in. |
Return Value:
The found string before the search string. Whitespaces at end will be removed.
Limit the number of words in a string. Put value of $end to the string end.
Str::limitWords( string $string, integer $limit = 10, string $end = '...' ): string
Related global function (description see above).
( jump back )
str_limit_words( string $string, int $limit = 10, string $end = '...' ): string
$string = 'The quick brown fox jumps over the lazy dog';
str_limit_words( $string, 3 );
// The quick brown...
Parameter | Type | Description |
---|---|---|
$string |
string | The string to limit the words. |
$limit |
integer | The number of words to limit. Defaults to 10. |
$end |
string | The string to end the cut string. Defaults to '...' |
Return Value:
The limited string with $end at the end.
Limit the number of characters in a string. Put value of $end to the string end.
Str::limit( string $string, integer $limit = 100, string $end = '...' ): string
Related global function (description see above).
( jump back )
str_limit( string $string, int $limit = 100, string $end = '...' ): string
$string = 'The quick brown fox jumps over the lazy dog';
str_limit( $string, 15 );
// The quick brown...
Parameter | Type | Description |
---|---|---|
$string |
string | The string to limit the characters. |
$limit |
integer | The number of characters to limit. Defaults to 100. |
$end |
string | The string to end the cut string. Defaults to '...' |
Return Value:
The limited string with $end at the end.
Tests if a string contains a given element
Str::contains( string|array $needle, string $haystack ): boolean
Related global function (description see above).
( jump back )
str_contains( string|array $needle, string $haystack ): boolean
$string = 'The quick brown fox jumps over the lazy dog';
$array = [
'cat',
'fox'
];
str_contains( $array, $string );
// bool(true)
Parameter | Type | Description |
---|---|---|
$needle |
string|array | A string or an array of strings. |
$haystack |
string | The string to search in. |
Return Value:
True if $needle is found, false otherwise.
Tests if a string contains a given element. Ignore case sensitivity.
Str::containsIgnoreCase( string|array $needle, string $haystack ): boolean
Related global function (description see above).
( jump back )
str_icontains( string|array $needle, string $haystack ): boolean
$string = 'The quick brown fox jumps over the lazy dog';
$array = [
'Cat',
'Fox'
];
str_icontains( $array, $string );
// bool(true)
Parameter | Type | Description |
---|---|---|
$needle |
string|array | A string or an array of strings. |
$haystack |
string | The string to search in. |
Return Value:
True if $needle is found, false otherwise.
Determine if a given string starts with a given substring.
Str::startsWith( string|array $needle, string $haystack ): boolean
Related global function (description see above).
( jump back )
str_starts_with( string|array $needle, string $haystack ): boolean
$string = 'The quick brown fox jumps over the lazy dog';
$array = [
'Cat',
'The'
];
str_starts_with( $array, $string );
// bool(true)
Parameter | Type | Description |
---|---|---|
$needle |
string|array | The string or array of strings to search for. |
$haystack |
string | The string to search in. |
Return Value:
True if $needle was found, false otherwise.
Determine if a given string starts with a given substring. Ignore case sensitivity.
Str::startsWithIgnoreCase( string|array $needle, string $haystack ): boolean
Related global function (description see above).
( jump back )
str_istarts_with( string|array $needle, string $haystack ): boolean
$string = 'The quick brown fox jumps over the lazy dog';
$array = [
'cat',
'the'
];
str_istarts_with( $array, $string );
// bool(true)
Parameter | Type | Description |
---|---|---|
$needle |
string|array | The string or array of strings to search for. |
$haystack |
string | The string to search in. |
Return Value:
True if $needle was found, false otherwise.
Determine if a given string ends with a given substring.
Str::endsWith( string|array $needle, string $haystack ): boolean
Related global function (description see above).
( jump back )
str_ends_with( string|array $needle, string $haystack ): boolean
$string = 'The quick brown fox jumps over the lazy dog';
$array = [
'cat',
'dog'
];
str_ends_with( $array, $string );
// bool(true)
Parameter | Type | Description |
---|---|---|
$needle |
string|array | The string or array of strings to search for. |
$haystack |
string | The string to search in. |
Return Value:
True if $needle was found, false otherwise.
Determine if a given string ends with a given substring.
Str::endsWithIgnoreCase( string|array $needle, string $haystack ): boolean
Related global function (description see above).
( jump back )
str_iends_with( string|array $needle, string $haystack ): boolean
$string = 'The quick brown fox jumps over the lazy dog';
$array = [
'Cat',
'Dog'
];
str_iends_with( $array, $string );
// bool(true)
Parameter | Type | Description |
---|---|---|
$needle |
string|array | The string or array of strings to search for. |
$haystack |
string | The string to search in. |
Return Value:
True if $needle was found, false otherwise.
Return the part of a string after the last occurrence of a given search value.
Str::afterLast( string $search, string $string ): string
Related global function (description see above).
( jump back )
str_after_last( string $search, string $string ): string
$path = "/var/www/html/public/img/image.jpg";
str_after_last( '/' $path );
// image.jpg
Parameter | Type | Description |
---|---|---|
$search |
string | The string to search for. |
$string |
string | The string to search in. |
Return Value:
The found string after the last occurrence of the search string. Whitespaces at beginning will be removed.
Helper class that provides easy access to useful common php functions.
Class Util
Validate a given email address.
Util::isEmail( string $email ): boolean
Related global function (description see above).
( jump back )
is_email( string $email ): boolean
$email = '[email protected]';
is_email( $email );
// bool(true)
Parameter | Type | Description |
---|---|---|
$email |
string | The email address to test. |
Return Value:
True if given string is a valid email address, false otherwise.
Get the current ip address of the user.
Util::ip( ): string|null
Related global function (description see above).
( jump back )
ip( ): null|string
echo ip();
// 127.0.0.1
Return Value:
The detected ip address, null if the ip was not detected.
Creates a secure hash from a given password. Uses the CRYPT_BLOWFISH algorithm.
Util::cryptPassword( string $password ): string
Note: 255 characters for database column recommended!
Related global function (description see above).
( jump back )
crypt_password( string $password ): string
$password = 'foobar';
crypt_password( $password );
// $2y$10$6qKwbwTgwQNcmcaw04eSf.QpP3.4T0..bEnY62dd1ozM8L61nb8AC
Parameter | Type | Description |
---|---|---|
$password |
string | The password to crypt. |
Return Value:
The crypted password.
Verifies that a password matches a crypted password (CRYPT_BLOWFISH algorithm).
Util::isPassword( string $password, string $cryptedPassword ): boolean
Related global function (description see above).
( jump back )
is_password( string $password, string $cryptedPassword ): boolean
$password = 'foobar';
$cryptedPassword = '$2y$10$6qKwbwTgwQNcmcaw04eSf.QpP3.4T0..bEnY62dd1ozM8L61nb8AC';
is_password( $password, $cryptedPassword );
// bool(true)
Parameter | Type | Description |
---|---|---|
$password |
string | The password to test. |
$cryptedPassword |
string | The crypted password (e.g. stored in the database). |
Dumps the content of the given variable and exits the script.
Util::dd( mixed $var )
Related global function (description see above).
( jump back )
dd( mixed $var )
$array = [
'foo' => 'bar',
'baz' => 'qux'
];
dd( $array );
// (
// [foo] => bar
// [baz] => qux
// )
Parameter | Type | Description |
---|---|---|
$var |
mixed | The var to dump. |
Dumps the content of the given variable. Script does NOT stop after call.
Util::dump( mixed $var )
Related global function (description see above).
( jump back )
dump( mixed $var )
$array = [
'foo' => 'bar',
'baz' => 'qux'
];
dump( $array );
// (
// [foo] => bar
// [baz] => qux
// )
Parameter | Type | Description |
---|---|---|
$var |
mixed | The var to dump. |
Helper class that provides easy access to useful php yml functions.
Class Yml
Validates if a given file contains yaml syntax.
Yml::isValidFile( string $file ): boolean
Related global function (description see above).
( jump back )
is_yml_file( string $file ): boolean
$file = /path/to/file.yml
is_yml_file( $file );
// bool(true)
Parameter | Type | Description |
---|---|---|
$file |
string | The file to test for yaml syntax. |
Return Value:
True if the file contains yaml syntax, false otherwise.
Tests if the syntax of a given string is yaml.
Yml::isValid( string $string ): boolean
Related global function (description see above).
( jump back )
is_yml( string $string ): boolean
$string = "
foo: bar
baz: qux
foobar:
foo: bar
";
is_yml( $string );
// bool(true)
Parameter | Type | Description |
---|---|---|
$string |
string | The string to test for yaml syntax. |
Return Value:
True if the string is yaml, false otherwise.
Transforms a given yaml string into an array.
Yml::parse( string $yml ): array|null
Related global function (description see above).
( jump back )
yml_parse( string $yml ): array|null
$yml = "
foo: bar
baz: qux
foobar:
foo: bar
";
yml_parse( $yml );
// (
// [foo] => bar
// [baz] => qux
// [foobar] => (
// [foo] => bar
// )
// )
Parameter | Type | Description |
---|---|---|
$yml |
string | The yaml string to parse. |
Return Value:
The transformed array, null on error.
Gets a value in a yaml string using the dot notation.
Yml::get( string $key, string $yml ): mixed
Related global function (description see above).
( jump back )
yml_get( string $key, string $yml ): mixed
$yml = "
foo: bar
baz: qux
foobar:
foo: bar
";
yml_get( 'foobar.foo', $yml );
// bar
Parameter | Type | Description |
---|---|---|
$key |
string | The key to search using dot notation (e.g. 'foo.bar.baz'). |
$yml |
string | The yml string to search in. |
Return Value:
The found value, null otherwise.
Gets a value in a yaml file using the dot notation.
Yml::getFile( string $key, string $ymlfile ): mixed
Related global function (description see above).
( jump back )
yml_get_file( string $key, string $ymlfile ): mixed
$ymlfile = '/path/to/file.yml';
yml_get_file( 'foobar.foo', $ymlfile );
// bar
Parameter | Type | Description |
---|---|---|
$key |
string | The key to search using dot notation (e.g. 'foo.bar.baz'). |
$ymlfile |
string | The ymlfile to search in. |
Return Value:
The found value, null otherwise.
Loads the content of a yamlfile into an array.
Yml::parseFile( string $ymlfile ): array
Related global function (description see above).
( jump back )
yml_parse_file( string $ymlfile ): array|null
$ymlfile = '/path/to/file.yml';
yml_parse_file( $ymlfile );
// (
// [foo] => bar
// [baz] => qux
// [foobar] => (
// [foo] => bar
// )
// )
Parameter | Type | Description |
---|---|---|
$ymlfile |
string | The path of the file to read from. |
Return Value:
The parsed array.
Sets a value in a yamlfile using the dot notation. Note: all comments in the file will be removed!
Yml::setFile( string $key, mixed $value, string $ymlfile ): boolean
Related global function (description see above).
( jump back )
yml_set_file( string $key, mixed $value, string $ymlfile ): boolean
$ymlfile = '/path/to/file.yml';
yml_set_file( 'foobar.foo', 'baz', $ymlfile );
// foo: bar
// baz: qux
// foobar:
// foo: baz
Parameter | Type | Description |
---|---|---|
$key |
string | The string to search with dot notation |
$value |
mixed | The value to set on the specified key. |
$ymlfile |
string | The ymlfile to set the value in. |
Return Value:
True if value was successfully set in yamlfile, false otherwise.
Transformes a given array to yaml syntax and puts its content into a given file. Note: if the file exists, it will be overwritten!
Yml::dumpFile( array|object $var, string $filename, integer $indent = 2, integer $wordwrap, boolean $openingDashes = false ): boolean
Related global function (description see above).
( jump back )
to_yml_file( array|object $var, string $filename, int $indent = 2, int $wordwrap = 0, bool $openingDashes = false ): boolean
$array = [
'foo' => 'bar',
'baz' => 'qux'
];
to_yml_file( $array, '/path/to/file.yml' );
// foo: bar
// baz: qux
Parameter | Type | Description |
---|---|---|
$var |
array|object | The array or object to transform. |
$filename |
string | The path to the file to write the yaml string into. Note: if the file already exists, it will be overwritten! |
$indent |
integer | The indent of the converted yaml. Defaults to 2. |
$wordwrap |
integer | After the given number a string will be wraped. Default to 0 (no wordwrap). |
$openingDashes |
boolean | True if the yaml string should start with opening dashes. Defaults to false. |
Return Value:
True on success, false otherwise.
Transformes a given array or object to a yaml string.
Yml::dump( array|object $var, integer $indent = 2, integer $wordwrap, boolean $openingDashes = false ): string|null
Related global function (description see above).
( jump back )
to_yml( array|object $array, string $filename, int $indent = 2, int $wordwrap = 0, bool $openingDashes = false ): string|null
$array = [
'foo' => 'bar',
'baz' => 'qux',
'foobar' => [
'foo' => 'bar'
]
];
to_yml( $array );
// foo: bar
// baz: qux
// foobar:
// foo: bar
Parameter | Type | Description |
---|---|---|
$var |
array|object | The array or object to transform. |
$indent |
integer | The indent of the converted yaml. Defaults to 2. |
$wordwrap |
integer | After the given number a string will be wraped. Default to 0 (no wordwrap). |
$openingDashes |
boolean | True if the yaml string should start with opening dashes. Defaults to false. |
Return Value:
The converted yaml string. On errors, null is returned.
Sets a value in a yaml string using the dot notation.
Yml::set( string $key, mixed $value, string &$yml ): boolean
Related global function (description see above).
( jump back )
yml_set( string $key, mixed $value, string &$yml ): boolean
$yml = "
foo: bar
baz: qux
foobar:
foo: bar
";
yml_set( 'foobar.foo', 'baz', $yml );
// foo: bar
// baz: qux
// foobar:
// foo: baz
Parameter | Type | Description |
---|---|---|
$key |
string | The string to search with dot notation |
$value |
mixed | The value to set on the specified key. |
$yml |
string | The yml string to search in. Note: all comments in the string will be removed! |
Return Value:
True if value was successfully set, false otherwise.
This document was automatically generated from source code comments on 2018-01-22 using phpDocumentor and cvuorinen/phpdoc-markdown-public