class Str (View source)

The Str class holds all string methods.

Methods

static string
dateFormat(string $date, string $format, string $locale = false)

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, boolean $multilineOnly = false)

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

static number
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, boolean $removeDots = false, number $maxChars = 100)

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

static string
shorten(string $str, number $maxChars, string $ellipsis = ' ...')

Shortens a string keeping full words. Note that this method also first strips all tags from the 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, string $locale = false)

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 $date
string $format
string $locale

Return Value

string The formatted date

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 false

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 false

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

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

Parameters

string $str
boolean $multilineOnly

Return Value

string The parsed string

static number match(string $str, string $regex)

Perform a regex match.

Parameters

string $str
string $regex

Return Value

number 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, boolean $removeDots = false, number $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
boolean $removeDots
number $maxChars

Return Value

string The sanitized string

static string shorten(string $str, number $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
number $maxChars
string $ellipsis

Return Value

string The shortened string

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