Provides Laravel-specific and pure PHP helper functions.
PHP >=5.6.4
Laravel >=5.2
-
Install package through
composer
:composer require illuminated/helper-functions
-
That's it! Now you can use any of provided helper functions.
Removes the given values from array:
$array = ['foo', 'bar', 'baz'];
$array = array_except_value($array, 'baz');
// ['foo', 'bar']
$array = ['foo', 'bar', 'baz'];
$array = array_except_value($array, ['bar', 'baz']);
// ['foo']
Calls artisan console command in background, with optional before
and after
sub-commands:
call_in_background('foo');
// "php artisan foo" would be called in background
call_in_background('foo:bar baz', 'sleep 0.3');
// "sleep 0.3 && php artisan foo:bar baz" would be called in background
Checks if default database connection is mysql
or not:
if (db_is_mysql()) {
// mysql-specific code here
}
Returns database datetime, using mysql
connection:
$now = db_mysql_now();
// 2016-06-23 15:23:16
Returns value of specified mysql
variable, or false
if variable doesn't exist:
$hostname = db_mysql_variable('hostname');
// localhost
Returns nicely formatted string representation of the variable, using Symfony VarDumper Component with all of it's benefits:
$array = [
'a simple string' => 'in an array of 5 elements',
'a float' => 1.0,
'an integer' => 1,
'a boolean' => true,
'an empty array' => [],
];
$dump = get_dump($array);
// array:5 [
// "a simple string" => "in an array of 5 elements"
// "a float" => 1.0
// "an integer" => 1
// "a boolean" => true
// "an empty array" => []
// ]
Checks if specified string is valid email address or not:
$isEmail = is_email('john.doe@example.com');
// true
Converts addresses data to RFC 2822 string, suitable for PHP mail() function:
$address = to_rfc2822_email([
['address' => 'john.doe@example.com', 'name' => 'John Doe'],
['address' => 'mary.smith@example.com'],
]);
// John Doe <john.doe@example.com>, mary.smith@example.com
Also supports simplified syntax for single address item:
$address = to_rfc2822_email(['address' => 'john.doe@example.com', 'name' => 'John Doe']);
// John Doe <john.doe@example.com>
Converts addresses data to format, which is suitable for SwiftMailer library:
$addresses = to_swiftmailer_emails([
['address' => 'john.doe@example.com', 'name' => 'John Doe'],
['address' => 'mary.smith@example.com'],
]);
// ["john.doe@example.com" => "John Doe", "mary.smith@example.com"]
Also supports simplified syntax for single address item:
$address = to_swiftmailer_emails(['address' => 'john.doe@example.com', 'name' => 'John Doe']);
// ["john.doe@example.com" => "John Doe"]
Formats bytes into kilobytes, megabytes, gigabytes or terabytes, with specified precision:
$formatted = format_bytes(3333333);
// 3.18 MB
Checks if specified variable is valid json-encoded string or not:
$isJson = is_json('{"foo":1,"bar":2,"baz":3}');
// true
Function can return decoded json, if you pass the second return
argument as true
:
$decoded = is_json('{"foo":1,"bar":2,"baz":3}', true);
// ['foo' => 1, 'bar' => 2, 'baz' => 3]
Converts string to lowercase:
$lower = str_lower('TeSt');
// test
Converts string to uppercase:
$upper = str_upper('TeSt');
// TEST