Str
class Str (View source)
The Str class holds all string methods.
Methods
Change format of a given date, optionally according to locale settings.
Set a default value for $str in case $str is empty.
Escapes a string to be used safely in a JSON string.
Find the URL of the first image within rendered HTML markup.
Find the first paragraph in rendered HTML and return its inner HTML.
Parse a markdown string. Optionally skip parsing in case $str is a single line string.
Perform a regex match.
Search and replace by regex.
Cleans up a string to be used as URL, directory or file name.
Shortens a string keeping full words. Note that this method also first strips all tags from the given string.
Creates a slug for save diretory names, ids or similar from a given string.
Strip substring from end of string.
Strip substring from start of string.
Removes all HTML and Markdown (!) tags.
Details
at line 71
static string
dateFormat(string $date, string $format = 'D, d M Y', string|null $locale = null)
Change format of a given date, optionally according to locale settings.
In case a date variable is set in a txt file, its format can be different to a timestamp (mtime) of a file or page. To be independent on the given format without explicitly specifying it, strtotime() is used generate a proper input date. To use DateTime::createFromFormat() instead would require a third parameter (the original format) and would therefore make things more complicated than needed. The format can use either the date() or ICU syntax. In case a locale is defined, the ICU syntax is used.
at line 101
static string
def(string $str, string $defaultValue = '')
Set a default value for $str in case $str is empty.
at line 115
static string
escape(string $str)
Escapes a string to be used safely in a JSON string.
at line 135
static string
findFirstImage(string $str)
Find the URL of the first image within rendered HTML markup.
at line 155
static string
findFirstParagraph(string $str)
Find the first paragraph in rendered HTML and return its inner HTML.
at line 178
static string
markdown(string $str, bool $multilineOnly = false)
Parse a markdown string. Optionally skip parsing in case $str is a single line string.
at line 200
static int
match(string $str, string $regex = '')
Perform a regex match.
at line 212
static string
replace(string $str, string $regex = '', string $replace = '')
Search and replace by regex.
at line 229
static string
sanitize(string $str, bool $removeDots = false, int $maxChars = 100)
Cleans up a string to be used as URL, directory or file name.
The returned string constists of the following characters: "a-z", "0-9", "-" and optional dots ".". That means, this method is safe to be used with filenames as well, since it keeps by default the dots as suffix separators.
Note: To produce fully safe prefixes and directory names, possible dots should be removed by setting $removeDots = true.
at line 266
static string
shorten(string $str, int $maxChars, string $ellipsis = ' ...')
Shortens a string keeping full words. Note that this method also first strips all tags from the given string.
at line 301
static string
slug(string $str, bool $removeDots = false, int $maxChars = 100)
Creates a slug for save diretory names, ids or similar from a given string.
In case the sanitized string is empty or the string is shorter than 6 chars while the input string is longer than 12 chars, the string is replaced with a md5 hash shortened to 16 chars.
at line 322
static string
stripEnd(string $str, string $end = '')
Strip substring from end of string.
at line 333
static string
stripStart(string $str, string $start = '')
Strip substring from start of string.
at line 343
static string
stripTags(string $str)
Removes all HTML and Markdown (!) tags.