class StringHelper implements ProtectedContextAwareInterface (View source)

String helpers for Eel contexts

Methods

string
substr(string $string, int $start, int $length = null)

Return the characters in a string from start up to the given length

string
substring(string $string, int $start, int $end = null)

Return the characters in a string from a start index to an end index

string
charAt(string $string, int $index)

Get the character at a specific position

bool
endsWith(string $string, string $search, int|null $position = null)

Test if a string ends with the given search string

string
chr(int $value)

Generate a single-byte string from a number

int
ord(string $string)

Convert the first byte of a string to a value between 0 and 255

int
indexOf(string $string, string $search, int $fromIndex = null)

Find the first position of a substring in the given string

int
lastIndexOf(string $string, string $search, int $toIndex = null)

Find the last position of a substring in the given string

array|null
pregMatch(string $string, string $pattern)

Match a string with a regular expression (PREG style)

array|null
pregMatchAll(string $string, string $pattern)

Perform a global regular expression match (PREG style)

string
pregReplace(string $string, string $pattern, string $replace, int $limit = -1)

Replace occurrences of a search string inside the string using regular expression matching (PREG style)

array
pregSplit(string $string, string $pattern, int $limit = -1)

Split a string by a separator using regular expression matching (PREG style)

array|string|string[]
replace(array|string|null $string, array|string|null $search, array|string|null $replace)

Replace occurrences of a search string inside the string

array
split(string $string, string|null $separator = null, int|null $limit = null)

Split a string by a separator

bool
startsWith(string $string, string $search, int $position = null)

Test if a string starts with the given search string

string
toLowerCase(string $string)

Lowercase a string

string
toUpperCase(string $string)

Uppercase a string

string
firstLetterToUpperCase(string $string)

Uppercase the first letter of a string

string
firstLetterToLowerCase(string $string)

Lowercase the first letter of a string

string
stripTags(string $string, string|null $allowableTags = null)

Strip all HTML tags from the given string

string
nl2br(string $string)

Insert HTML line breaks before all newlines in a string

bool
isBlank(string $string)

Test if the given string is blank (empty or consists of whitespace only)

string
trim(string $string, string $charlist = null)

Trim whitespace at the beginning and end of a string

string
toString(mixed $value)

Convert the given value to a string

int
toInteger(string $string)

Convert a string to integer

float
toFloat(string $string)

Convert a string to float

bool
toBoolean(string $string)

Convert a string to boolean

string
rawUrlEncode(string $string)

Encode the string for URLs according to RFC 3986

string
rawUrlDecode(string $string)

Decode the string from URLs according to RFC 3986

string
htmlSpecialChars(string $string, bool $preserveEntities = false)

Convert special characters to HTML entities

string
crop(string $string, int $maximumCharacters, string $suffix = '')

Crop a string to maximumCharacters length, optionally appending suffix if cropping was necessary.

string
cropAtWord(string $string, int $maximumCharacters, string $suffix = '')

Crop a string to maximumCharacters length, taking words into account, optionally appending suffix if cropping was necessary.

string
cropAtSentence(string $string, int $maximumCharacters, string $suffix = '')

Crop a string to maximumCharacters length, taking sentences into account, optionally appending suffix if cropping was necessary.

string
md5(string $string)

Calculate the MD5 checksum of the given string

string
sha1(string $string)

Calculate the SHA1 checksum of the given string

int
length(string $string)

Get the length of a string

int
wordCount(string $unicodeString)

Return the count of words for a given string. Remove marks & digits and flatten all kind of whitespaces (tabs, new lines and multiple spaces) For example this helper can be utilized to calculate the reading time of an article.

string
base64encode(string $string)

Implementation of the PHP base64_encode function

string|bool
base64decode(string $string, bool $strict = false)

Implementation of the PHP base64_decode function

string
format(string $format, array $args)

Implementation of the PHP vsprintf function

bool
allowsCallOfMethod(string $methodName)

All methods are considered safe

Details

string substr(string $string, int $start, int $length = null)

Return the characters in a string from start up to the given length

This implementation follows the JavaScript specification for "substr".

Examples::

String.substr('Hello, World!', 7, 5) == 'World'
String.substr('Hello, World!', 7) == 'World!'
String.substr('Hello, World!', -6) == 'World!'

Parameters

string $string

A string

int $start

Start offset

int $length

Maximum length of the substring that is returned

Return Value

string

The substring

string substring(string $string, int $start, int $end = null)

Return the characters in a string from a start index to an end index

This implementation follows the JavaScript specification for "substring".

Examples::

String.substring('Hello, World!', 7, 12) == 'World'
String.substring('Hello, World!', 7) == 'World!'

Parameters

string $string
int $start

Start index

int $end

End index

Return Value

string

The substring

string charAt(string $string, int $index)

Get the character at a specific position

Example::

String.charAt("abcdefg", 5) == "f"

Parameters

string $string

The input string

int $index

The index to get

Return Value

string

The character at the given index

bool endsWith(string $string, string $search, int|null $position = null)

Test if a string ends with the given search string

Example::

String.endsWith('Hello, World!', 'World!') == true

Parameters

string $string

The string

string $search

A string to search

int|null $position

Optional position for limiting the string

Return Value

bool

true if the string ends with the given search

string chr(int $value)

Generate a single-byte string from a number

Example::

String.chr(65) == "A"

This is a wrapper for the chr() PHP function.

Parameters

int $value

An integer between 0 and 255

Return Value

string

A single-character string containing the specified byte

See also

ord()

int ord(string $string)

Convert the first byte of a string to a value between 0 and 255

Example::

String.ord('A') == 65

This is a wrapper for the ord() PHP function.

Parameters

string $string

A character

Return Value

int

An integer between 0 and 255

See also

chr()

int indexOf(string $string, string $search, int $fromIndex = null)

Find the first position of a substring in the given string

Example::

String.indexOf("Blue Whale", "Blue") == 0

Parameters

string $string

The input string

string $search

The substring to search for

int $fromIndex

The index where the search should start, defaults to the beginning

Return Value

int

The index of the substring (>= 0) or -1 if the substring was not found

int lastIndexOf(string $string, string $search, int $toIndex = null)

Find the last position of a substring in the given string

Example::

String.lastIndexOf("Developers Developers Developers!", "Developers") == 22

Parameters

string $string

The input string

string $search

The substring to search for

int $toIndex

The position where the backwards search should start, defaults to the end

Return Value

int

The last index of the substring (>=0) or -1 if the substring was not found

array|null pregMatch(string $string, string $pattern)

Match a string with a regular expression (PREG style)

Example::

String.pregMatch("For more information, see Chapter 3.4.5.1", "/(chapter \d+(.\d)*)/i") == ['Chapter 3.4.5.1', 'Chapter 3.4.5.1', '.1']

Parameters

string $string

The input string

string $pattern

A PREG pattern

Return Value

array|null

The matches as array or NULL if not matched

Exceptions

EvaluationException

array|null pregMatchAll(string $string, string $pattern)

Perform a global regular expression match (PREG style)

Example::

String.pregMatchAll("



", '/id="icon-(.+?)"/') == [['id="icon-one"', 'id="icon-two"'],['one','two']]

Parameters

string $string

The input string

string $pattern

A PREG pattern

Return Value

array|null

The matches as array or NULL if not matched

Exceptions

EvaluationException

string pregReplace(string $string, string $pattern, string $replace, int $limit = -1)

Replace occurrences of a search string inside the string using regular expression matching (PREG style)

Examples::

String.pregReplace("Some.String with sp:cial characters", "/[[:^alnum:]]/", "-") == "Some-String-with-sp-cial-characters" String.pregReplace("Some.String with sp:cial characters", "/[[:^alnum:]]/", "-", 1) == "Some-String with sp:cial characters" String.pregReplace("2016-08-31", "/([0-9]+)-([0-9]+)-([0-9]+)/", "$3.$2.$1") == "31.08.2016"

Parameters

string $string

The input string

string $pattern

A PREG pattern

string $replace

A replacement string, can contain references to capture groups with "\n" or "$n"

int $limit

The maximum possible replacements for each pattern in each subject string. Defaults to -1 (no limit).

Return Value

string

The string with all occurrences replaced

array pregSplit(string $string, string $pattern, int $limit = -1)

Split a string by a separator using regular expression matching (PREG style)

Examples::

String.pregSplit("foo bar baz", "/\s+/") == ['foo', 'bar', 'baz'] String.pregSplit("first second third", "/\s+/", 2) == ['first', 'second third']

Parameters

string $string

The input string

string $pattern

A PREG pattern

int $limit

The maximum amount of items to return, in contrast to split() this will return all remaining characters in the last item (see example)

Return Value

array

An array of the splitted parts, excluding the matched pattern

array|string|string[] replace(array|string|null $string, array|string|null $search, array|string|null $replace)

Replace occurrences of a search string inside the string

Example::

String.replace("canal", "ana", "oo") == "cool"
String.replace("cool gridge", ["oo", "gri"], ["ana", "bri"]) == "canal bridge"

Note: this method does not perform regular expression matching, pregReplace().

Parameters

array|string|null $string

The input string

array|string|null $search

A search string

array|string|null $replace

A replacement string

Return Value

array|string|string[]

The string with all occurrences replaced

array split(string $string, string|null $separator = null, int|null $limit = null)

Split a string by a separator

Example::

String.split("My hovercraft is full of eels", " ") == ['My', 'hovercraft', 'is', 'full', 'of', 'eels']
String.split("Foo", "", 2) == ['F', 'o']

Node: This implementation follows JavaScript semantics without support of regular expressions.

Parameters

string $string

The string to split

string|null $separator

The separator where the string should be splitted

int|null $limit

The maximum amount of items to split (exceeding items will be discarded)

Return Value

array

An array of the splitted parts, excluding the separators

bool startsWith(string $string, string $search, int $position = null)

Test if a string starts with the given search string

Examples::

String.startsWith('Hello world!', 'Hello') == true String.startsWith('My hovercraft is full of...', 'Hello') == false String.startsWith('My hovercraft is full of...', 'hovercraft', 3) == true

Parameters

string $string

The input string

string $search

The string to search for

int $position

The position to test (defaults to the beginning of the string)

Return Value

bool

string toLowerCase(string $string)

Lowercase a string

Parameters

string $string

The input string

Return Value

string

The string in lowercase

string toUpperCase(string $string)

Uppercase a string

Parameters

string $string

The input string

Return Value

string

The string in uppercase

string firstLetterToUpperCase(string $string)

Uppercase the first letter of a string

Example::

String.firstLetterToUpperCase('hello world') == 'Hello world'

Parameters

string $string

The input string

Return Value

string

The string with the first letter in uppercase

string firstLetterToLowerCase(string $string)

Lowercase the first letter of a string

Example::

String.firstLetterToLowerCase('CamelCase') == 'camelCase'

Parameters

string $string

The input string

Return Value

string

The string with the first letter in lowercase

string stripTags(string $string, string|null $allowableTags = null)

Strip all HTML tags from the given string

Example::

String.stripTags('<a href="#">Some link</a>') == 'Some link'

This is a wrapper for the strip_tags() PHP function.

Parameters

string $string

The string to strip

string|null $allowableTags

Specify tags which should not be stripped

Return Value

string

The string with tags stripped

string nl2br(string $string)

Insert HTML line breaks before all newlines in a string

Example::

String.nl2br(someStingWithLinebreaks) == 'line1<br />line2'

This is a wrapper for the nl2br() PHP function.

Parameters

string $string

The input string

Return Value

string

The string with new lines replaced

bool isBlank(string $string)

Test if the given string is blank (empty or consists of whitespace only)

Examples::

String.isBlank('') == true String.isBlank(' ') == true

Parameters

string $string

The string to test

Return Value

bool

true if the given string is blank

string trim(string $string, string $charlist = null)

Trim whitespace at the beginning and end of a string

Parameters

string $string

The string to trim

string $charlist

List of characters that should be trimmed, defaults to whitespace

Return Value

string

The trimmed string

string toString(mixed $value)

Convert the given value to a string

Parameters

mixed $value

The value to convert (must be convertible to string)

Return Value

string

The string value

int toInteger(string $string)

Convert a string to integer

Parameters

string $string

The string to convert

Return Value

int

The converted string

float toFloat(string $string)

Convert a string to float

Parameters

string $string

The string to convert

Return Value

float

The float value of the string

bool toBoolean(string $string)

Convert a string to boolean

A value is true, if it is either the string "true" or "TRUE" or the number 1.

Parameters

string $string

The string to convert

Return Value

bool

The boolean value of the string (true or false)

string rawUrlEncode(string $string)

Encode the string for URLs according to RFC 3986

Parameters

string $string

The string to encode

Return Value

string

The encoded string

string rawUrlDecode(string $string)

Decode the string from URLs according to RFC 3986

Parameters

string $string

The string to decode

Return Value

string

The decoded string

string htmlSpecialChars(string $string, bool $preserveEntities = false)

Convert special characters to HTML entities

Parameters

string $string

The string to convert

bool $preserveEntities

true if entities should not be double encoded

Return Value

string

The converted string

string crop(string $string, int $maximumCharacters, string $suffix = '')

Crop a string to maximumCharacters length, optionally appending suffix if cropping was necessary.

Parameters

string $string

The input string

int $maximumCharacters

Number of characters where cropping should happen

string $suffix

Suffix to be appended if cropping was necessary

Return Value

string

The cropped string

string cropAtWord(string $string, int $maximumCharacters, string $suffix = '')

Crop a string to maximumCharacters length, taking words into account, optionally appending suffix if cropping was necessary.

Parameters

string $string

The input string

int $maximumCharacters

Number of characters where cropping should happen

string $suffix

Suffix to be appended if cropping was necessary

Return Value

string

The cropped string

string cropAtSentence(string $string, int $maximumCharacters, string $suffix = '')

Crop a string to maximumCharacters length, taking sentences into account, optionally appending suffix if cropping was necessary.

Parameters

string $string

The input string

int $maximumCharacters

Number of characters where cropping should happen

string $suffix

Suffix to be appended if cropping was necessary

Return Value

string

The cropped string

string md5(string $string)

Calculate the MD5 checksum of the given string

Example::

String.md5("joh316") == "bacb98acf97e0b6112b1d1b650b84971"

Parameters

string $string

The string to hash

Return Value

string

The MD5 hash of string

string sha1(string $string)

Calculate the SHA1 checksum of the given string

Example::

String.sha1("joh316") == "063b3d108bed9f88fa618c6046de0dccadcf3158"

Parameters

string $string

The string to hash

Return Value

string

The SHA1 hash of string

int length(string $string)

Get the length of a string

Parameters

string $string

The input string

Return Value

int

Length of the string

int wordCount(string $unicodeString)

Return the count of words for a given string. Remove marks & digits and flatten all kind of whitespaces (tabs, new lines and multiple spaces) For example this helper can be utilized to calculate the reading time of an article.

Parameters

string $unicodeString

The input string

Return Value

int

Number of words

string base64encode(string $string)

Implementation of the PHP base64_encode function

Parameters

string $string

The data to encode.

Return Value

string

The encoded data

See also

https://php.net/manual/en/function.base64-encode.php

string|bool base64decode(string $string, bool $strict = false)

Implementation of the PHP base64_decode function

Parameters

string $string

The encoded data.

bool $strict

If TRUE this function will return FALSE if the input contains character from outside the base64 alphabet.

Return Value

string|bool

The decoded data or FALSE on failure. The returned data may be binary.

See also

https://php.net/manual/en/function.base64-decode.php

string format(string $format, array $args)

Implementation of the PHP vsprintf function

Parameters

string $format

A formatting string containing directives

array $args

An array of values to be inserted according to the formatting string $format

Return Value

string

A string produced according to the formatting string $format

See also

https://php.net/manual/en/function.vsprintf.php

bool allowsCallOfMethod(string $methodName)

All methods are considered safe

Parameters

string $methodName

Return Value

bool