-
Notifications
You must be signed in to change notification settings - Fork 0
Formatting
This class contains functions to help (re)format data.
Use:
\blobfolio\common\format (by value)
\blobfolio\common\ref\format (by reference)
Rebuild a key=>value array so that each value is an array containing the original key and value. This can be helpful if exporting associative array data to Javascript, for example.
- By Value
- By Reference
| Type | Description |
|---|---|
array |
Array. |
If passing by value a new array is returned, otherwise TRUE.
$arr = array(
'fruit'=>'apple',
'vegetable'=>'carrot'
);
\blobfolio\common\format::array_to_indexed($arr);
/*
array(
0 => array(
[key] => fruit,
[value] => apple
),
1 => array(
[key] => vegetable,
[value] => carrot
)
)
*/Convert a CIDR to a minimum/maximum range of IPs.
| Type | Description |
|---|---|
string |
CIDR. |
Returns an array with "min" and "max" IPs or FALSE on failure.
\blobfolio\common\format::cidr_to_range('2600:3c00::f03c:91ff:feae:0ff2/64');
/*
array(
[min] => 2600:3c00::f03c:91ff:feae:ff2,
[max] => 2600:3c00::ffff:ffff:ffff:ffff
)
*/This is just like PHP's ceil() function, except that you can specify a decimal precision.
- By Value
- By Reference
| Type | Description | Notes | Default |
|---|---|---|---|
mixed |
Number(s). | If an array is passed, each value will be processed recursively. | |
int |
Precision. | 0 |
If passing by value the rounded value is returned as a float, otherwise TRUE.
Convert as many HTML entities to their character counterpoints as possible with as many passes as it takes to get them all.
- By Value
- By Reference
| Type | Description |
|---|---|
string |
HTML. |
If passing by value a decoded string is returned, otherwise TRUE.
\blobfolio\common\format::decode_entities('"Happy Days"'); // "Happy Days"Converts the following back to actual characters: \b, \f, \n, \r, \t.
- By Value
- By Reference
| Type | Description |
|---|---|
string |
String. |
If passing by value a decoded string is returned, otherwise TRUE.
Shorthand for running both ::decode_unicode_entities() and ::decode_escape_entities().
- By Value
- By Reference
| Type | Description |
|---|---|
string |
String. |
If passing by value a decoded string is returned, otherwise TRUE.
Converts Unicode like \u0000 to an actual character.
- By Value
- By Reference
| Type | Description |
|---|---|
string |
String. |
If passing by value a decoded string is returned, otherwise TRUE.
Shorten text to a set number of letters or words. This function is multi-byte safe provided mbstring is present.
| Type | Description | Notes |
|---|---|---|
string |
String. | |
array |
Options. | See below. |
Options:
| Type | Description | Notes | Default |
|---|---|---|---|
int |
Length. | The maximum string length. | 200 |
string |
Suffix. | A suffix to indicate that the string has been shortened. | "…" |
string |
Unit. | Either "character" or "word". |
"character" |
Returns the original or truncated string.
$str = "Hey good lookin'";
$foo = \blobfolio\common\format::excerpt($str, array('length'=>5, 'unit'=>'character')); // Hey g…
$foo = \blobfolio\common\format::excerpt($str, array('length'=>2, 'unit'=>'word')); // Hey good…This is just like PHP's floor() function, except that you can specify a decimal precision.
- By Value
- By Reference
| Type | Description | Notes | Default |
|---|---|---|---|
mixed |
Number(s). | If an array is passed, each value will be processed recursively. | |
int |
Precision. | 0 |
If passing by value the rounded value is returned as a float, otherwise TRUE.
Choose between a singular and plural string given a numerical value. printf() formatting is supported.
| Type | Description | Notes |
|---|---|---|
mixed |
Count. | If an array is passed, its count() will be used. Otherwise the value should be numeric. |
string |
Singular. | |
string |
Plural. |
Returns the singular string if the count is 1, otherwise the plural string.
echo "I have " . \blobfolio\common\format::inflect(1, '%d book', '%d books'); // I have 1 bookConvert an IPv4 or IPv6 address to its numerical equivalent. This requires the PHP extensions bcmath or gmp.
- By Value
- By Reference
| Type | Description |
|---|---|
string |
IP address. |
If passed by value, returns the numerical IP address or FALSE, otherwise TRUE/FALSE.
// By value.
$foo = \blobfolio\common\format::ip_to_number('50.116.18.174'); // 846467758
// By reference.
\blobfolio\common\ref\format::ip_to_number($foo);Retrieve the subnet range for an IP address. This assumes /24 for IPv4 and /64 for IPv6. This requires the PHP extensions bcmath or gmp.
- By Value
- By Reference
| Type | Description |
|---|---|
string |
IP address. |
If passed by value, returns the subnet or FALSE, otherwise TRUE/FALSE.
// By value.
$foo = \blobfolio\common\format::ip_to_subnet('50.116.18.174'); // 50.116.18.0/24
// By reference.
\blobfolio\common\ref\format::ip_to_subnet($foo);Convert a JSON or JSON-like string into proper JSON. This will fix key/value quoting, remove comments, remove trailing commas, correct UTF-8 issues, etc.
- By Value
- By Reference
| Type | Description | Notes | Default |
|---|---|---|---|
mixed |
Data. | If a JSON(ish) string is passed, its formatting will be sanitized/corrected; if something else is passed, it will be converted to JSON. | |
bool |
Pretty print. | If TRUE, output will be pretty-printed. |
TRUE |
If passed by value, returns JSON string or NULL, otherwise TRUE/FALSE.
A json_decode() wrapper that applies the same formatting forgiveness as the above ::json() function, giving the data a fighting chance of coming through.
Note: This always returns objecty data as an associative array.
- By Value
- By Reference
| Type | Description |
|---|---|
string |
JSON. |
If passed by value, returns the decoded data, otherwise TRUE/FALSE.
Convert plain-text URLs, domains, email addresses, and telephone numbers into clickable (HTML) links.
Malformed data will be left alone, as will any non-FQDN domain or email address.
Unicode hosts are supported if the PHP extension intl is installed, but the resulting href attribute will be converted to Punycode/ASCII.
People are very inconsistent at how they write telephone numbers. This looks specifically for international formatting (+1 201-555-0123), or common North American 10-digit representations like (201) 555-0123, 201-555-0123, 201.555.0123, etc. Other types of numbery bits, or phone-ish strings that don't validate, will be ignored.
- By Value
- By Reference
| Type | Description | Notes |
|---|---|---|
string |
String. | |
array |
Attributes. | Attributes to add to generated links. See below. |
All attributes are optional.
| Type | Key | Description |
|---|---|---|
string, array
|
class |
Class(es). |
string |
rel |
Relation. |
string |
target |
Target. |
If passed by value, returns the text with added links, otherwise TRUE.
// By value.
$foo = \blobfolio\common\format::links('Welcome to domain.com!'); // Welcome to <a href="http://domain.com">domain.com</a>!
$args = array(
'target'=>'_blank'
);
$foo = \blobfolio\common\format::links('me@domain.com', $args); // <a href="mailto:me@domain.com" target="_blank">me@domain.com</a>
// By reference.
\blobfolio\common\ref\format::links($foo);Convert a delimited list, or array of delimited lists, into a normal, single-dimensional array.
This is useful, e.g., in parsing function arguments that might be passed as proper arrays or comma-delimited strings.
Note: empty values are always ignored, but other types of falsey values are fine.
- By Value
- By Reference
| Type | Description | Notes |
|---|---|---|
mixed |
List. | |
array |
Options. | See below for details. |
If passed by value, returns an array of values, otherwise TRUE.
// Possible arguments, defaults shown.
$args = array(
'delimiter'=>',', // List delimiter.
'cast'=>'string', // Cast output values as this type.
'trim'=>true, // Trim values, so e.g. "1, 2" and "1,2" wind up the same.
'min'=>null, // Remove values less than this.
'max'=>null, // Remove values greater than this.
'unique'=>true, // Remove duplicate values.
'sort'=>false, // Sort values.
);
// By value.
$foo = \blobfolio\common\format::list_to_array('1,,2,3', $args); // [1, 2, 3]
$args = array(
'min'=>'2'
);
$foo = \blobfolio\common\format::list_to_array('1,2,3', $args); // [2,3]
// By reference.
blobfolio\common\ref\format::list_to_array($foo, $args);Format a value as US currency.
- By Value
- By Reference
| Type | Description | Notes | Default |
|---|---|---|---|
mixed |
Amount. | If an array is passed, each value will be processed recursively. | |
bool |
Cents. | If TRUE, sub-dollar values will be formatted like "50¢". |
FALSE |
string |
Thousands separator. | "" |
|
bool |
Remove trailing .00. |
If TRUE, cents will be stripped off whole-dollar amounts. |
FALSE |
If passing by value, returns the formatted amount, otherwise TRUE.
// By value.
$foo = \blobfolio\common\format::money(.75, true); // 75¢
$foo = \blobfolio\common\format::money(.75, false); // $0.75
// By reference.
\blobfolio\common\ref\format::money($foo);Like long2ip but capable of handling IPv6. This requires the PHP extensions bcmath or gmp.
- By Value
- By Reference
| Type | Description |
|---|---|
int, string
|
Decimal IP address. |
If passed by value, returns the IP address or FALSE, otherwise TRUE/FALSE.
// By value.
$foo = \blobfolio\common\format::number_to_ip(846467758); // 50.116.18.174
// By reference.
\blobfolio\common\ref\format::number_to_ip($foo);Format and verify a phone number using international formatting. This uses blob-phone for extra-strength goodness.
- By Value
- By Reference
| Type | Description | Notes | Default |
|---|---|---|---|
string |
Phone number. | ||
string |
(Suspected) Country. | Providing the suspected country of origin helps with identification, as there is some overlap in formatting rules. | "US" |
array |
Type(s) | Require specific type(s) of number. See below. | NULL |
Phone number types:
-
"fixed", i.e. landline "mobile""pager""personal_number"-
"premium_rate", e.g. your favorite party line "shared_cost""toll_free""voicemail"-
"voip", e.g. Google Voice
Returns "" if the number is invalid, otherwise the number in proper international format. If passing by reference TRUE or FALSE is returned.
// By value.
$foo = \blobfolio\common\format::phone('(555) 618-2086'); // +1 555-608-2086
// By reference.
\blobfolio\common\ref\format::phone($foo);This is just like PHP's round() function, except that you can pass an array of values to process recursively if desired.
- By Value
- By Reference
| Type | Description | Notes | Default |
|---|---|---|---|
mixed |
Number(s). | If an array is passed, each value will be processed recursively. | |
int |
Precision. | 0 |
|
int |
Rounding mode. | See round() for possible values. | PHP_ROUND_HALF_UP |
If passing by value the rounded value is returned as a float, otherwise TRUE.
Convert a dataset to CSV format.
| Type | Description | Notes | Default |
|---|---|---|---|
array |
Data. | This should be an array of arrays, the outer being rows, the inner being columns. | |
array |
Headers. | A header row will be inserted using these values, if provided, or the associative keys of the first data row, if applicable. |
NULL |
string |
Column separator. | "," |
|
string |
Row separator | "\n" |
Returns a string containing the CSV content.
$data = array(
array('John','Doe','01/01/2000'),
array('Jane','Doe','12/25/1998')
);
$headers = array('First', 'Last', 'Joined');
$csv = \blobfolio\common\format::to_csv($data, $headers);Convert a datestring from one timezone to another.
- By Value
- By Reference
| Type | Description | Default |
|---|---|---|
int, string
|
Date or timestamp. | |
string |
From TZ. | "UTC" |
string |
To TZ. | "UTC" |
// By value.
$foo = \blobfolio\common\format::to_timezone('2015:01:01 10:00:00', 'UTC', 'America/Los_Angeles'); // 2015-01-01 02:00:00
// By reference.
\blobfolio\common\ref\format::to_timezone($foo, 'UTC', 'America/Chicago');Convert a dataset to Microsoft Excel's XML format. Special cell formatting will be used for boolean, numeric, percent, currency, and date values.
| Type | Description | Notes | Default |
|---|---|---|---|
array |
Data. | This should be an array of arrays, the outer being rows, the inner being columns. | |
array |
Headers. | A header row will be inserted using these values, if provided, or the associative keys of the first data row, if applicable. |
NULL |
Returns a string containing the XML/XLS content.
$data = array(
array('John','Doe','01/01/2000'),
array('Jane','Doe','12/25/1998')
);
$headers = array('First', 'Last', 'Joined');
$xls = blobfolio\common\format::to_xls($data, $headers);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