StringUtility
Class with helper functions for string handling
Table of Contents
Methods
- base64urlDecode() : string
- 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
- beginsWith() : bool
- Returns TRUE if $haystack begins with $needle.
- cast() : string|null
- Casts applicable types (string, bool, finite numeric) to string.
- endsWith() : bool
- Returns TRUE if $haystack ends with $needle.
- escapeCssSelector() : string
- Escape a CSS selector to be used for DOM queries
- 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. This method supports UTF-8 encoded strings only!
- 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) : 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
-
base64url decoded string
Return values
string —raw value
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
beginsWith()
Returns TRUE if $haystack begins with $needle.
public
static beginsWith(string $haystack, string $needle) : bool
will be removed in TYPO3 v12.0. Use PHP's native str_starts_with() function instead.
The input string is not trimmed before and search is done case sensitive.
Parameters
- $haystack : string
-
Full string to check
- $needle : string
-
Reference string which must be found as the "first part" of the full string
Tags
Return values
bool —TRUE if $needle was found to be equal to the first part of $haystack
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|nullendsWith()
Returns TRUE if $haystack ends with $needle.
public
static endsWith(string $haystack, string $needle) : bool
will be removed in TYPO3 v12.0. Use PHP's native str_ends_with() function instead.
The input string is not trimmed before and search is done case sensitive.
Parameters
- $haystack : string
-
Full string to check
- $needle : string
-
Reference string which must be found as the "last part" of the full string
Tags
Return values
bool —TRUE if $needle was found to be equal to the last part of $haystack
escapeCssSelector()
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
stringfilter()
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. This method supports UTF-8 encoded strings only!
public
static removeByteOrderMark(string $input) : string
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
-
Accept a comma-separated list of values.
Return values
string —Returns the list without any duplicates of values, space around values are trimmed.