class Str (View source)

The Str class holds all string methods.

Methods

static string
dateFormat(string|null $date, string|null $format = null, string|null $locale = null)

Change format of a given date, optionally according to locale settings.

static string
def(string|null $str, string|null $defaultValue = null)

Set a default value for $str in case $str is empty.

static string
escape(string|null $str)

Escapes a string to be used safely in a JSON string.

static string
findFirstImage(string|null $str)

Find the URL of the first image within rendered HTML markup.

static string
findFirstParagraph(string|null $str)

Find the first paragraph in rendered HTML and return its inner HTML.

static string
markdown(string|null $str, bool $multilineOnly = false)

Parse a markdown string. Optionally skip parsing in case $str is a single line string.

static int
match(string|null $str, string|null $regex = null)

Perform a regex match.

static string
replace(string|null $str, string|null $regex = null, string|null $replace = null)

Search and replace by regex.

static string
sanitize(string|null $str, bool $removeDots = false, int $maxChars = 100)

Cleans up a string to be used as URL, directory or file name.

static string
shorten(string|null $str, int $maxChars, string $ellipsis = ' ...')

Shortens a string keeping full words. Note that this method also first strips all tags from the given string.

static string
slug(string|null $str, bool $removeDots = false, int $maxChars = 100)

Creates a slug for save diretory names, ids or similar from a given string.

static string
stripEnd(string|null $str, string|null $end = null)

Strip substring from end of string.

static string
stripStart(string|null $str, string|null $start = null)

Strip substring from start of string.

static string
stripTags(string|null $str)

Removes all HTML and Markdown (!) tags.

Details

static string dateFormat(string|null $date, string|null $format = null, 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 strftime() syntax. In case a locale is defined, the strftime() syntax has to be used. Multiple values can be passed as a CSV string for the locale parameter.

Parameters

string|null $date
string|null $format
string|null $locale

Return Value

string The formatted date

static string def(string|null $str, string|null $defaultValue = null)

Set a default value for $str in case $str is empty.

Parameters

string|null $str
string|null $defaultValue

Return Value

string The default value

static string escape(string|null $str)

Escapes a string to be used safely in a JSON string.

Parameters

string|null $str

Return Value

string The escaped string

static string findFirstImage(string|null $str)

Find the URL of the first image within rendered HTML markup.

Parameters

string|null $str

Return Value

string The URL of the first image or false

static string findFirstParagraph(string|null $str)

Find the first paragraph in rendered HTML and return its inner HTML.

Parameters

string|null $str

Return Value

string The inner HTML of the first paragraph or false

static string markdown(string|null $str, bool $multilineOnly = false)

Parse a markdown string. Optionally skip parsing in case $str is a single line string.

Parameters

string|null $str
bool $multilineOnly

Return Value

string The parsed string

static int match(string|null $str, string|null $regex = null)

Perform a regex match.

Parameters

string|null $str
string|null $regex

Return Value

int 1 or 0

static string replace(string|null $str, string|null $regex = null, string|null $replace = null)

Search and replace by regex.

Parameters

string|null $str
string|null $regex
string|null $replace

Return Value

string The processed string

static string sanitize(string|null $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.

Parameters

string|null $str
bool $removeDots
int $maxChars

Return Value

string The sanitized string

static string shorten(string|null $str, int $maxChars, string $ellipsis = ' ...')

Shortens a string keeping full words. Note that this method also first strips all tags from the given string.

Parameters

string|null $str
int $maxChars
string $ellipsis

Return Value

string The shortened string

static string slug(string|null $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.

Parameters

string|null $str
bool $removeDots
int $maxChars

Return Value

string the slug

static string stripEnd(string|null $str, string|null $end = null)

Strip substring from end of string.

Parameters

string|null $str
string|null $end

Return Value

string The processed string

static string stripStart(string|null $str, string|null $start = null)

Strip substring from start of string.

Parameters

string|null $str
string|null $start

Return Value

string The processed string

static string stripTags(string|null $str)

Removes all HTML and Markdown (!) tags.

Parameters

string|null $str

Return Value

string The clean string