-
Notifications
You must be signed in to change notification settings - Fork 0
Multi Byte Wrappers
This class contains various multi-byte versions of native PHP functions. In order to benefit from these, the mbstring
extension must be installed.
Use:
\blobfolio\common\mb
(by value)
\blobfolio\common\ref\mb
(by reference)
Parse a query string, e.g. "foo=bar&apples=oranges"
.
Type | Description | Notes |
---|---|---|
string |
String. | |
mixed |
Result. | Variable to store output by reference. |
N/A
Break a URL down into its constituent parts. Aside from adding Unicode support, this wrapper will:
- Fix scheme-agnostic URLs like
"//domain.com"
; - Treat a schemeless host as a host rather than a path;
- Compact IPv6 hosts;
- Punycode conversion of Unicode hosts;
This function requires intl
for international/Unicode domain support, and either bcmath
or gmp
for proper IP address handling.
Note: as with the native parse_url
, this function does not fully validate URL hosts.
Type | Description | Notes | Default |
---|---|---|---|
string |
URL. | ||
int |
Component. | If provided, a single component is returned. See parse_url() for options. | -1 |
Returns an array of the URL parts if $component
is omitted, otherwise the component or NULL
is returned.
$foo = \blobfolio\common\mb::parse_url('http://☺.com', PHP_URL_HOST); // xn--74h.com
$foo = \blobfolio\common\mb::parse_url('http://☺.com/party-time/');
/*
array(
scheme => http
host => xn--74h.com
path => /party-time/
)
*/
Split the characters in a string by the desired length.
- By Value
- By Reference
Type | Description | Default |
---|---|---|
string |
String. | |
int |
Chunk length. | 1 |
Returns an array of characters by value, TRUE
/FALSE
by reference.
$foo = 'quEen BjöRk Ⅷ loVes aPplEs.';
\blobfolio\common\mb::str_split($foo, 5);
/*
array(
0 => quEen
1 => BjöR
...
)
*/
Count the number of characters in a string.
Type | Description |
---|---|
string |
String. |
Returns the size of the string as an integer. If mbstring
is not installed, the byte size will be returned instead.
Pad a string so it meets a required length.
- By Value
- By Reference
Type | Description | Notes | Default |
---|---|---|---|
string |
String. | ||
int |
Width. | ||
string |
Pad string. | " " |
|
int |
Pad type. | Either STR_PAD_LEFT , STR_PAD_BOTH , or STR_PAD_RIGHT . |
STR_PAD_RIGHT |
Returns the padded string by value or TRUE
/FALSE
by reference. If the desired width is less than the string or negative, the string will be returned as-was. If the pad string doesn't divide evenly into the pad width, it may be truncated.
$foo = 'quEen BjöRk Ⅷ loVes aPplEs.';
echo \blobfolio\common\mb::str_pad($foo, 50, '<>', STR_PAD_LEFT));
echo \blobfolio\common\mb::str_pad($foo, 50, '<>', STR_PAD_BOTH));
echo \blobfolio\common\mb::str_pad($foo, 50, '<>', STR_PAD_RIGHT));
/*
><><><><><><><><><><><>quEen BjöRk Ⅷ loVes aPplEs.
<><><><><><>quEen BjöRk Ⅷ loVes aPplEs.<><><><><><
quEen BjöRk Ⅷ loVes aPplEs.<><><><><><><><><><><><
*/
Find the first occurrence of the needle in the haystack.
Type | Description | Default |
---|---|---|
string |
Haystack. | |
string |
Needle. | |
int |
Offset. | 0 |
Returns the starting position of the needle of FALSE
if not found.
Reverse a string, e.g. "abc"
to "cba"
.
- By Value
- By Reference
Type | Description |
---|---|
string |
String. |
Returns the reversed string by value, otherwise TRUE
/FALSE
.
Find the last occurrence of the needle in the haystack.
Type | Description | Default |
---|---|---|
string |
Haystack. | |
string |
Needle. | |
int |
Offset. | 0 |
Returns the last starting position of the needle of FALSE
if not found.
Lowercase a string. This function additionally catches various Unicode characters with upper/lower variants that mbstring
doesn't bother checking for.
- By Value
- By Reference
Type | Description | Notes | Default |
---|---|---|---|
string , array
|
String. | If an array is passed, each value will be processed recursively. | |
bool |
Strict. | If TRUE , non-string types will be ignored, otherwise they'll be typecast to strings. |
FALSE |
If passing by value, the lowercased string is returned, otherwise TRUE
.
$foo = 'quEen BjöRk Ⅷ loVes aPplEs.';
echo \blobfolio\common\mb::strtolower($foo); // queen björk ⅷ loves apples.
Uppercase a string. This function additionally catches various Unicode characters with upper/lower variants that mbstring
doesn't bother checking for.
- By Value
- By Reference
Type | Description | Notes | Default |
---|---|---|---|
string , array
|
String. | If an array is passed, each value will be processed recursively. | |
bool |
Strict. | If TRUE , non-string types will be ignored, otherwise they'll be typecast to strings. |
FALSE |
If passing by value, the uppercased string is returned, otherwise TRUE
.
$foo = 'quEen BjöRk Ⅷ loVes aPplEs.';
echo \blobfolio\common\mb::strtoupper($foo); // QUEEN BJÖRK Ⅷ LOVES APPLES.
Retrieve a portion of the string.
Type | Description | Default |
---|---|---|
string |
String. | |
int |
Start. | 0 |
int |
Length. | NULL |
Returns the matching substring.
Count the number of occurrences of the needle in the haystack.
Type | Description |
---|---|
string |
Haystack. |
string |
Needle. |
Returns the number of matches.
Actually trim all leading and trailing whitespace from a string.
- By Value
- By Reference
Type | Description | Notes |
---|---|---|
string , array
|
String. | If an array is passed, each value will be processed recursively. |
Returns the trimmed string if by value, TRUE
otherwise.
Sentence-case a string. This function additionally catches various Unicode characters with upper/lower variants that mbstring
doesn't bother checking for.
- By Value
- By Reference
Type | Description | Notes | Default |
---|---|---|---|
string , array
|
String. | If an array is passed, each value will be processed recursively. | |
bool |
Strict. | If TRUE , non-string types will be ignored, otherwise they'll be typecast to strings. |
FALSE |
If passing by value, the sentence-cased string is returned, otherwise TRUE
.
$foo = 'quEen BjöRk Ⅷ loVes aPplEs.';
echo \blobfolio\common\mb::ucfirst($foo); // QuEen BjöRk Ⅷ loVes aPplEs.
Title-case a string. This function additionally catches various Unicode characters with upper/lower variants that mbstring
doesn't bother checking for, and will also adjust letters following dashes and forward slashes.
- By Value
- By Reference
Type | Description | Notes | Default |
---|---|---|---|
string , array
|
String. | If an array is passed, each value will be processed recursively. | |
bool |
Strict. | If TRUE , non-string types will be ignored, otherwise they'll be typecast to strings. |
FALSE |
If passing by value, the title-cased string is returned, otherwise TRUE
.
$foo = 'quEen BjöRk Ⅷ loVes aPplEs.';
echo \blobfolio\common\mb::ucwords($foo); // Queen Björk Ⅷ Loves Apples.
Wrap long lines. Unlike PHP's version, this takes hyphenation and dashes into account when deciding whether to wrap or split a word.
- By Value
- By Reference
Type | Description | Notes | Default |
---|---|---|---|
string |
String. | ||
int |
Line Width. | 75 |
|
string |
EOL. | "\n" |
|
bool |
Break words. | If TRUE , words exceeding the line width will be cut, otherwise they'll be inserted as-is, making for an extra long line. |
FALSE |
Returns the wrapped string by value, TRUE
/FALSE
by reference.
$foo = "Björk's dress is attention-getting.";
\blobfolio\common\mb::wordwrap($foo, 15);
/*
Björk's dress
is attention-
getting.
*/
// By comparison, the PHP's native function does not try to split on hyphens.
wordwrap($foo, 15);
/*
Björk's dress
is
attention-getting.
*/
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