StringUtility
Class with helper functions for string handling
Table of Contents
Methods
- base64urlDecode() : string|false
- Returns base64 decoded value with a URL and filename safe alphabet according to https://tools.ietf.org/html/rfc4648#section-5
- base64urlEncode() : string
- Returns base64 encoded value with a URL and filename safe alphabet according to https://tools.ietf.org/html/rfc4648#section-5
- cast() : string|null
- Casts applicable types (string, bool, finite numeric) to string.
- escapeCssSelector() : string
- Escape a CSS selector to be used for DOM queries
- explodeEscaped() : array<string|int, mixed>
- Explodes a string while respecting escape characters
- filter() : string|null
- Keeps only string types (filters out non-strings).
- getUniqueId() : string
- This function generates a unique id by using the more entropy parameter.
- multibyteStringPad() : string
- Works the same as str_pad() except that it correctly handles strings with multibyte characters and takes an additional optional argument $encoding.
- removeByteOrderMark() : string
- Removes the Byte Order Mark (BOM) from the input string.
- searchStringWildcard() : bool
- Matching two strings against each other, supporting a "*" wildcard (match many) or a "?" wildcard (match one= or (if wrapped in "/") PCRE regular expressions
- uniqueList() : string
- Takes a comma-separated list and removes all duplicates.
Methods
base64urlDecode()
Returns base64 decoded value with a URL and filename safe alphabet according to https://tools.ietf.org/html/rfc4648#section-5
public
static base64urlDecode(string $value[, bool $strict = false ]) : string|false
The difference to classic base64 is, that the result
alphabet is adjusted like shown below, padding (=
)
is stripped completely:
- position #62:
-
(minus) ->+
- position #63:
_
(underscore) ->/
Parameters
- $value : string
-
base64url decoded string
- $strict : bool = false
-
enforces to only allow characters contained in the base64(url) alphabet
Return values
string|false —raw value, or false
if non-base64(url) characters were given in strict mode
base64urlEncode()
Returns base64 encoded value with a URL and filename safe alphabet according to https://tools.ietf.org/html/rfc4648#section-5
public
static base64urlEncode(string $value) : string
The difference to classic base64 is, that the result
alphabet is adjusted like shown below, padding (=
)
is stripped completely:
- position #62:
+
->-
(minus) - position #63:
/
->_
(underscore)
Parameters
- $value : string
-
raw value
Return values
string —base64url encoded string
cast()
Casts applicable types (string, bool, finite numeric) to string.
public
static cast(mixed $value[, string|null $default = null ]) : string|null
Any other type will be replaced by the $default
value.
Parameters
- $value : mixed
- $default : string|null = null
Return values
string|nullescapeCssSelector()
Escape a CSS selector to be used for DOM queries
public
static escapeCssSelector(string $selector) : string
This method takes care to escape any CSS selector meta character. The result may be used to query the DOM like $('#' + escapedSelector)
Parameters
- $selector : string
Return values
stringexplodeEscaped()
Explodes a string while respecting escape characters
public
static explodeEscaped(string $delimiter, string $subject[, string $escapeCharacter = '\' ]) : array<string|int, mixed>
e.g.: delimiter: '.'; escapeCharacter: ''; subject: 'new.site.child' result: [new.site, child]
Parameters
- $delimiter : string
- $subject : string
- $escapeCharacter : string = '\'
Return values
array<string|int, mixed>filter()
Keeps only string types (filters out non-strings).
public
static filter(mixed $value[, string|null $default = null ]) : string|null
Any other non-string type will be replaced by the $default
value.
Parameters
- $value : mixed
- $default : string|null = null
Return values
string|nullgetUniqueId()
This function generates a unique id by using the more entropy parameter.
public
static getUniqueId([string $prefix = '' ]) : string
Furthermore, the dots are removed so the id can be used inside HTML attributes e.g. id.
Parameters
- $prefix : string = ''
Return values
stringmultibyteStringPad()
Works the same as str_pad() except that it correctly handles strings with multibyte characters and takes an additional optional argument $encoding.
public
static multibyteStringPad(string $string, int $length[, string $pad_string = ' ' ][, int $pad_type = STR_PAD_RIGHT ][, string $encoding = 'UTF-8' ]) : string
Parameters
- $string : string
- $length : int
- $pad_string : string = ' '
- $pad_type : int = STR_PAD_RIGHT
- $encoding : string = 'UTF-8'
Return values
stringremoveByteOrderMark()
Removes the Byte Order Mark (BOM) from the input string.
public
static removeByteOrderMark(string $input) : string
This method supports UTF-8 encoded strings only!
Parameters
- $input : string
Return values
stringsearchStringWildcard()
Matching two strings against each other, supporting a "*" wildcard (match many) or a "?" wildcard (match one= or (if wrapped in "/") PCRE regular expressions
public
static searchStringWildcard(string $haystack, string $needle) : bool
Parameters
- $haystack : string
-
The string in which to find $needle.
- $needle : string
-
The string to find in $haystack
Return values
bool —Returns TRUE if $needle matches or is found in (according to wildcards) $haystack. E.g. if $haystack is "Netscape 6.5" and $needle is "Net*" or "Net*ape" then it returns TRUE.
uniqueList()
Takes a comma-separated list and removes all duplicates.
public
static uniqueList(string $list) : string
If a value in the list is trim(empty), the value is ignored.
Parameters
- $list : string
-
A comma-separated list of values.
Return values
string —Returns the list without any duplicates of values, space around values are trimmed.