StringHelper
class StringHelper implements ProtectedContextAwareInterface (View source)
String helpers for Eel contexts
Methods
Return the characters in a string from start up to the given length
Return the characters in a string from a start index to an end index
Get the character at a specific position
Test if a string ends with the given search string
Generate a single-byte string from a number
Convert the first byte of a string to a value between 0 and 255
Find the first position of a substring in the given string
Find the last position of a substring in the given string
Match a string with a regular expression (PREG style)
Perform a global regular expression match (PREG style)
Replace occurrences of a search string inside the string using regular expression matching (PREG style)
Split a string by a separator using regular expression matching (PREG style)
Replace occurrences of a search string inside the string
Split a string by a separator
Test if a string starts with the given search string
Lowercase a string
Uppercase a string
Uppercase the first letter of a string
Lowercase the first letter of a string
Strip all HTML tags from the given string
Insert HTML line breaks before all newlines in a string
Test if the given string is blank (empty or consists of whitespace only)
Trim whitespace at the beginning and end of a string
Convert the given value to a string
Convert a string to integer
Convert a string to float
Convert a string to boolean
Encode the string for URLs according to RFC 3986
Decode the string from URLs according to RFC 3986
Convert special characters to HTML entities
Crop a string to maximumCharacters
length, optionally appending suffix
if cropping was necessary.
Crop a string to maximumCharacters
length, taking words into account,
optionally appending suffix
if cropping was necessary.
Crop a string to maximumCharacters
length, taking sentences into account,
optionally appending suffix
if cropping was necessary.
Calculate the MD5 checksum of the given string
Calculate the SHA1 checksum of the given string
Get the length of a string
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.
Implementation of the PHP base64_encode function
Implementation of the PHP base64_decode function
Implementation of the PHP vsprintf function
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!'
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!'
string
charAt(string $string, int $index)
Get the character at a specific position
Example::
String.charAt("abcdefg", 5) == "f"
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
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.
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.
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
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
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']
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']]
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"
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']
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().
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.
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
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
Example::
String.firstLetterToUpperCase('hello world') == 'Hello world'
string
firstLetterToLowerCase(string $string)
Lowercase the first letter of a string
Example::
String.firstLetterToLowerCase('CamelCase') == 'camelCase'
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.
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.
bool
isBlank(string $string)
Test if the given string is blank (empty or consists of whitespace only)
Examples::
String.isBlank('') == true String.isBlank(' ') == true
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
A value is true
, if it is either the string "true"
or "TRUE"
or the number 1
.
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
Example::
String.md5("joh316") == "bacb98acf97e0b6112b1d1b650b84971"
string
sha1(string $string)
Calculate the SHA1 checksum of the given string
Example::
String.sha1("joh316") == "063b3d108bed9f88fa618c6046de0dccadcf3158"
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