My personal helpers
These are some of the functions I use intensively for my projects., (*1)
As I don't want my functions to interfere with any of yours, they all use the Embryo
namespace., (*2)
Array helpers
arrayDepth
This function returns the depth of a given array.
- 0 if the array is empty
- 1 for unidimensional arrays
- 2 to x for every other array types, (*3)
WARNING: a recursive array could currently cause an infinite loop (See issue #29)., (*4)
Command Line Interface (CLI) helpers
These helpers are made specifically for command-line interfaces (CLI), (*5)
cliArguments
Just add $args = Embryo\cliArguments();
at the beginning of your CLI script, and use your CLI script as you do with a bash
script., (*6)
php myScript.php -action makeSomething -verbose
The array $args
will now contain :
- action: 'makeSomething'
- verbose: true, (*7)
You can also give an array of allowed arguments in the first parameter of cliArguments()
. Any parameter other than
the ones listed will generate and error and die., (*8)
cliClear
Calling this function clears the command line interface.
Tested with Windows and Unix environments., (*9)
cliIsInterface
Tells whether the current script is called in a command-line interface., (*10)
cliProgressBar
Displays a nice progressBar., (*11)
Since the display is updated less frequently than the spinner, it is slightly quicker than cliSpinner
., (*12)
foreach($myvar as $var) {
Embryo\cliProgressBar($currentIndex, $total);
}
cliPrompt
Prompts a question to the user, and returns a boolean: true
if the answer is the first letter or the first answer,
false
in every other case., (*13)
If no answers are set, default answers are [y]es / [n]o
, (*14)
Embryo\cliPrompt('Do you want to continue ?');
Will display Do you want to continue ? [y]es / [n]o
, and will return true if the answer is either y
or yes
., (*15)
Embryo\cliPrompt('Do you want to continue ?', ['continue', 'abort']);
Will display Do you want to continue ? [c]ontinue / [a]bort
, and will return true if the answer is either c
or
continue
., (*16)
cliQuestion
Displays a question to the user, and returns its answer as a string., (*17)
$answer = Embryo\cliQuestion('What is the airspeed velocity of an unladen swallow ?');
cliSpinner
Adds a nice spinner to your loops., (*18)
foreach($myvar as $var) {
Embryo\cliSpinner('Manipulating data');
// Do something
}
Embryo\cliSpinner('Done !', true);
cliTable
Displays an array as a readable table., (*19)
By default, the first value's keys will be used as header names., (*20)
$table = [
[
'id' => 1,
'name' => 'John Doe',
'email' => 'john@doe.com',
],
[
'id' => 2,
'name' => 'Jane Doe',
'email' => 'jane@doe.com',
],
];
echo Embryo\cliTable($table);
will be displayed like this :, (*21)
ββββββ€βββββββββββ€βββββββββββββββ
β id β name β email β
ββββββΌβββββββββββΌβββββββββββββββ’
β 1 β John Doe β john@doe.com β
β 2 β Jane Doe β jane@doe.com β
ββββββ§βββββββββββ§βββββββββββββββ
Debug helpers
d
Dumps a variable. The second parameters allows to dump only if the corresponding $_REQUEST ($_GET or $_POST) parameter
exists, and is equivalent to true., (*22)
\Embryo\d($myVar);
dd
Like \Embryo\d()
, this function dumps a variable. It also dies just after., (*23)
If nothing has been sent to the browser before the call, and the data is an array or an object, a json header will be sent
and the data will be displayed as a json string., (*24)
pp
Pretty prints a given value by wrapping a var_dump into <pre>
tags, (*25)
SEO helpers
seoUrl
Creates an URL from any UTF-8 compatible given string., (*26)
It removes the accents, and replaces all non-alphanumeric character by hyphens., (*27)
$url = \Embryo\seoUrl("I'm giving my rΓ©sumΓ© to the cafΓ©, SeΓ±or !");
// $url equals 'i-m-giving-my-resume-to-the-cafe-senor'
unparseUrl
This method is intended to be a reverse of PHP's builtin parse_url.
The parsed url's query
key can be a string or an array., (*28)
string helpers
strBase64ImageEncode
Returns the correct base64 value for a given image path., (*29)
strComplete
Completes a string to a given length. If the length is shorter than the string, it will return the full, non-altered string., (*30)
$str = \Embryo\strComplete('test', 10, ' ');
// $str is 'test '
strCut
Cuts a text at a given number of characters., (*31)
If $isTotalLength
is set to true
, the final maximum length will be $length
. If it set to false, the final maximum
length will be $length + strlen($end)
., (*32)
If $length is >= 40, the function will not cut into a word, but just after the previous word., (*33)
strIsFourByteUtf8
Tells whether a string contains four-byte UTF-8 characters, (*34)
strIsJson
Tells whether a given string is valid JSON, (*35)
strIsUtf8
Tells whether a given string is encoded in UTF-8., (*36)
strIsXml
Returns whether the given string contains valid XML code (including HTML), (*37)
strRemoveAccents
Removes all accented characters from a given string., (*38)
strRemoveFourByteUtf8Characters
Removes all four-bytes UTF-8 characters from a given UTF-8 string., (*39)
It can be used to prevent Illegal mix of collation
errors in your database queries, for example, if your database is not set to UTF8MB4., (*40)