class Str (View source)

The Str class holds all string methods.

Methods

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.

static string
def(string $str, string $defaultValue = '')

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

static string
escape(string $str)

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

static string
findFirstImage(string $str)

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

static string
findFirstParagraph(string $str)

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

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

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

static int
match(string $str, string $regex = '')

Perform a regex match.

static string
replace(string $str, string $regex = '', string $replace = '')

Search and replace by regex.

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

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

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.

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.

static string
stripEnd(string $str, string $end = '')

Strip substring from end of string.

static string
stripStart(string $str, string $start = '')

Strip substring from start of string.

static string
stripTags(string $str)

Removes all HTML and Markdown (!) tags.

Details

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.

static string def(string $str, string $defaultValue = '')

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

Parameters

string $str
string $defaultValue

Return Value

string The default value

static string escape(string $str)

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

Parameters

string $str

Return Value

string The escaped string

static string findFirstImage(string $str)

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

Parameters

string $str

Return Value

string The URL of the first image or an empty string

static string findFirstParagraph(string $str)

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

Parameters

string $str

Return Value

string The inner HTML of the first paragraph or an empty string

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

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

Parameters

string $str
bool $multilineOnly

Return Value

string The parsed string

static int match(string $str, string $regex = '')

Perform a regex match.

Parameters

string $str
string $regex

Return Value

int 1 or 0

static string replace(string $str, string $regex = '', string $replace = '')

Search and replace by regex.

Parameters

string $str
string $regex
string $replace

Return Value

string The processed string

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.

Parameters

string $str
bool $removeDots
int $maxChars

Return Value

string The sanitized string

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.

Parameters

string $str
int $maxChars
string $ellipsis

Return Value

string The shortened string

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.

Parameters

string $str
bool $removeDots
int $maxChars

Return Value

string the slug

static string stripEnd(string $str, string $end = '')

Strip substring from end of string.

Parameters

string $str
string $end

Return Value

string The processed string

static string stripStart(string $str, string $start = '')

Strip substring from start of string.

Parameters

string $str
string $start

Return Value

string The processed string

static string stripTags(string $str)

Removes all HTML and Markdown (!) tags.

Parameters

string $str

Return Value

string The clean string