-
Notifications
You must be signed in to change notification settings - Fork 0
General Data Helpers
This class contains miscellaneous functionality, mostly related to data manipulation.
Use:
\blobfolio\common\data
Determine whether the contents (keys and values) of two arrays are equal. Sorting is ignored.
| Type | Description |
|---|---|
array |
Array one. |
array |
Array two. |
Returns TRUE if both arrays are equal, FALSE if they differ or if either are not an array.
$arr1 = array('apples','bananas','carrots');
$arr2 = array('bananas','carrots','apples');
var_dump(blobfolio\common\data::array_compare($arr1, $arr2)); // TRUEA case-insensitive version of array_diff(). Unlike array_diff(), however, type matters. That is, 2 !== "2", but "TWO" === "two".
| Type | Description |
|---|---|
array |
Array one. |
array |
Array two. |
| ... | Additional array(s). |
Returns an array containing items from the first array that were not present in any of the others.
A case-insensitive version of array_intersect(). Unlike array_intersect(), however, type matters. That is, 2 !== "2", but "TWO" === "two".
| Type | Description |
|---|---|
array |
Array one. |
array |
Array two. |
| ... | Additional arrays. |
Returns an array containing items from the first array that were present in all others.
Works exactly like array_key_exists() but is case-insensitive.
| Type | Description |
|---|---|
string |
Needle. |
array |
Haystack. |
Returns TRUE or FALSE.
Works exactly like array_search() but is case-insensitive.
| Type | Description | Default |
|---|---|---|
string |
Needle. | |
array |
Haystack. | |
bool |
Strict. | FALSE |
Returns the matching array key or FALSE if not found.
Recursively apply a callback function to the contents of an array.
| Type | Description |
|---|---|
callable |
Callback function. |
array |
Array. |
Returns the filtered array.
Convert an indefinitely long associative array of numeric values to one of a fixed length, with any extra summed under "other". This is useful for, e.g., pie chart stats, where spatial limitations make it impractical to list zillions of entries individually.
Note: US dollars, cents, and percentages are fine, but will be converted to floats, losing their string labels.
| Type | Description | Notes | Default |
|---|---|---|---|
array |
Array. | ||
int |
Length. | The number of items in the array returned will be less than or equal to this value. | 5 |
string |
Group label. | This is the key given to grouped values. | "Other" |
Returns an array of the defined length, greatest to least, with overages grouped under the other label at the end of the array.
$stats = array(
'US'=>100,
'CA'=>200,
'CN'=>5,
'GB'=>10,
'MX'=>30
);
$foo = blobfolio\common\data::array_otherize($stats, 3, 'ZZ');
/*
array(
CA => 200
US => 100
ZZ => 45
)
*/Return the last element of an array, but without altering the array as the true array_pop() does.
| Type | Description | Notes |
|---|---|---|
array |
Array. | By reference. |
Returns the last value in the array, or FALSE if the array is empty.
$arr = array('apples','bananas','carrots');
$last = blobfolio\common\data::array_pop($arr);
echo $last; // carrotsReturn a random value from the array. This works for both associative or sequential arrays.
| Type | Description | Notes |
|---|---|---|
array |
Array. | By reference. |
Returns a random value from the array, or FALSE if the array is empty.
$arr = array('apples','bananas','carrots');
echo blobfolio\common\data::array_pop_rand($arr); // apples
echo blobfolio\common\data::array_pop_rand($arr); // carrotsReturn the first element of an array, again without altering the original variable.
| Type | Description | Notes |
|---|---|---|
array |
Array. | By reference. |
Returns the first value in the array, or FALSE if the array is empty.
$arr = array('apples','bananas','carrots');
$first = blobfolio\common\data::array_pop_top($arr);
echo $first; // applesGenerate an array of months for e.g. use in a checkout form.
| Type | Description | Default |
|---|---|---|
string |
Value format. | "m - M" |
Returns an array of expiration months, keyed 1-12, values corresponding to the passed date format.
\blobfolio\common\data::cc_exp_months();
/*
array(
1 => 01 - Jan,
2 => 02 - Feb,
...
)
*/Generate an array of years for e.g. use in a checkout form.
| Type | Description | Default |
|---|---|---|
int |
Length. | 10 |
Returns an array of expiration years beginning with the current year.
\blobfolio\common\data::cc_exp_years();
/*
array(
2010 => 2010,
2011 => 2011,
...
)
*/Calculate the number of days between two dates. This will use \DateTime if available, but will fall back to counting up the seconds between the timestamps.
| Type | Description |
|---|---|
int, string
|
Date or timestamp. |
int, string
|
Date or timestamp. |
Returns the number of days between the two values. If the dates are equal or either are invalid, 0 is returned.
echo \blobfolio\common\data::datediff('2015-01-01', '2015-01-02'); // 1Works exactly like in_array() but is case-insensitive.
| Type | Description | Default |
|---|---|---|
string |
Needle. | |
array |
Haystack. | |
bool |
Strict. | FALSE |
Returns TRUE or FALSE.
Determine whether a value falls within a given range.
| Type | Description | Notes | Default |
|---|---|---|---|
mixed |
Value. | ||
mixed |
Minimum value. | If NULL, no minimum will be checked. |
NULL |
mixed |
Maximum value. | If NULL, no maximum will be checked. |
NULL |
Returns TRUE or FALSE. Only passed boundaries are tested.
var_dump(\blobfolio\common\data::in_range(3, 0, 300)); // TRUEDetermine if an IP address is within the range.
| Type | Description | Notes | Default |
|---|---|---|---|
string |
IP address. | ||
string |
Minimum IP or a CIDR. | ||
string |
Maximum IP. | This argument is ignored if a CIDR is passed above. | NULL |
Returns TRUE or FALSE.
// Test against a CIDR.
\blobfolio\common\data::in_range('127.0.0.1', '127.0.0.0/24'); // TRUE
// Test against two IPs.
\blobfolio\common\data::in_range('127.0.0.1', '127.0.0.0', '127.0.0.255'); // TRUEDetermine whether a string represents JSON data.
| Type | Description | Notes | Default |
|---|---|---|---|
string |
String. | ||
bool |
Allow empty. | If TRUE, empty strings will be considered valid JSON. |
FALSE |
Returns TRUE or FALSE.
Determine whether a string is UTF-8.
| Type | Description |
|---|---|
string |
String. |
Returns TRUE or FALSE.
Decode a JSON string, make sure the result is an array, and optionally format its values against a template. See also: [parse_args()][#parse_args].
| Type | Description | Notes | Default |
|---|---|---|---|
string |
JSON. | ||
array |
Defaults. | ||
bool |
Strict. | If TRUE, provided JSON values will be typecast according to the corresponding default entries. |
TRUE |
bool |
Recursive. | If TRUE, provided JSON values will be parsed recursively when the corresponding default is an associative array. |
TRUE |
Returns an array, optionally parsed according to the provided defaults.
$json = '{"fruit":"apple","pet":dog}';
$defaults = array(
'fruit'=>'banana',
'vegetable'=>'carrot'
);
print_r(\blobfolio\common\data::json_decode_array($json, $defaults));
/*
array(
fruit => apple,
vegetable => carrot
)
*/Determine whether a string's length falls within a given range of characters. This function is multi-byte safe provided mbstring is present.
| Type | Description | Notes | Default |
|---|---|---|---|
string |
String. | ||
int |
Minimum length. | If NULL, no minimum will be checked. |
NULL |
int |
Maximum length. | If NULL, no maximum will be checked. |
NULL |
Returns TRUE or FALSE.
var_dump(\blobfolio\common\data::length_in_range('The dog is cute.', 1, 10)); // FALSEGenerate an array combining user arguments and expected defaults. The structure of the default takes priority; foreign keys from the user arguments are not included in the output.
| Type | Description | Notes | Default |
|---|---|---|---|
array |
Arguments. | ||
array |
Defaults. | ||
bool |
Strict. | If TRUE, provided argument values will be typecast according to the corresponding default entries. |
TRUE |
bool |
Recursive. | If TRUE, provided argument values will be parsed recursively when the corresponding default is an associative array. |
TRUE |
Returns an array matching the default, with overrides courtesy of the passed arguments.
$args = array(
'fruit'=>'apple',
'pet'=>'dog'
);
$defaults = array(
'fruit'=>'banana',
'vegetable'=>'carrot'
);
print_r(\blobfolio\common\data::parse_args($args, $defaults));
/*
array(
fruit => apple,
vegetable => carrot
)
*/Generate a random integer using random_int() if available, otherwise mt_rand().
| Type | Description | Default |
|---|---|---|
int |
Minimum. | 0 |
int |
Maximum | 1 |
Returns a random integer between the bounds.
Generate a random string.
| Type | Description | Notes | Default |
|---|---|---|---|
int |
Length. | 10 |
|
array |
Alphabet. | If not provided, the random string will be built using unambiguous uppercase letters and numbers (e.g. skipping things like "I" or 1). |
NULL |
Returns a random string of the specified length.
Switch the values of two variables.
| Type | Description | Notes |
|---|---|---|
mixed |
Variable one. | By reference. |
mixed |
Variable two. | By reference. |
Returns TRUE.
$a = 'Hello';
$b = 'World';
\blobfolio\common\data::switcheroo($a, $b);
echo "$a $b"; // World HelloDelete a cookie and unset the corresponding $_COOKIE superglobal. Note: this must be run before headers have been sent. For best results, the arguments should match those used during the original setcookie() call.
| Type | Description | Default |
|---|---|---|
string |
Name. | |
string |
Path. | "" |
string |
Domain. | "" |
bool |
Secure. | FALSE |
bool |
HTTP-only. | FALSE |
Returns TRUE or FALSE. TRUE does not necessarily mean the cookie was successfully deleted, but rather that no show-stopping errors were thrown while trying.
// Set a cookie.
setcookie('foobar', 'Hello World');
// Unset it.
\blobfolio\common\data::unsetcookie('foobar');CLI
Constants
Dom Helpers
- ::get_nodes_by_class()
- ::innerhtml()
- ::load_svg()
- ::parse_css()
- ::remove_namespace()
- ::remove_node()
- ::remove_nodes()
- ::save_svg()
Files and Paths
- ::copy()
- ::csv_headers()
- ::data_uri()
- ::dirsize()
- ::empty_dir()
- ::hash_dir()
- ::leadingslash()
- ::line_count()
- ::mkdir()
- ::path()
- ::readfile_chunked()
- ::redirect()
- ::rmdir()
- ::scandir()
- ::trailingslash()
- ::unixslash()
- ::unleadingslash()
- ::unparse_url()
- ::untrailingslash()
Formatting
- ::array_flatten()
- ::array_to_indexed()
- ::ceil()
- ::cidr_to_range()
- ::decode_entities()
- ::decode_escape_entities()
- ::decode_js_entities()
- ::decode_unicode_entities()
- ::excerpt()
- ::floor()
- ::fraction()
- ::inflect()
- ::ip_to_number()
- ::ip_to_subnet()
- ::json()
- ::json_decode()
- ::json_encode()
- ::links()
- ::list_to_array()
- ::money()
- ::number_to_ip()
- ::phone()
- ::round()
- ::to_csv()
- ::to_timezone()
- ::to_xls()
General Data Helpers
- ::array_compare()
- ::array_idiff()
- ::array_iintersect()
- ::array_ikey_exists()
- ::array_isearch()
- ::array_map_recursive()
- ::array_otherize()
- ::array_pop()
- ::array_pop_rand()
- ::array_pop_top()
- ::cc_exp_months()
- ::cc_exp_years()
- ::datediff()
- ::iin_array()
- ::in_range()
- ::ip_in_range()
- ::is_json()
- ::is_utf8()
- ::json_decode_array()
- ::length_in_range()
- ::parse_args()
- ::random_int()
- ::random_string()
- ::switcheroo()
- ::unsetcookie()
Images
Multi-Byte Wrappers
- ::parse_str()
- ::parse_url()
- ::str_pad()
- ::str_split()
- ::strlen()
- ::strpos()
- ::strrev()
- ::strrpos()
- ::strtolower()
- ::strtoupper()
- ::substr()
- ::substr_count()
- ::trim()
- ::ucfirst()
- ::ucwords()
- ::wordwrap()
Sanitizing and Validation
- ::accents()
- ::attribute_value()
- ::au_state()
- ::ca_postal_code()
- ::cc()
- ::control_characters()
- ::country()
- ::csv()
- ::date()
- ::datetime()
- ::domain()
- ::ean()
- ::email()
- ::file_extension()
- ::html()
- ::hostname()
- ::ip()
- ::iri_value()
- ::isbn()
- ::js()
- ::mime()
- ::name()
- ::password()
- ::printable()
- ::province()
- ::quotes()
- ::state()
- ::svg()
- ::timezone()
- ::to_range()
- ::upc()
- ::url()
- ::utf8()
- ::whitespace()
- ::whitespace_multiline()
- ::zip5()
Typecasting