String Helper
StringHelper::truncate()
/**
* Truncates a string to the given length. It will optionally preserve
* HTML tags if $isHtml is set to true.
*
* @param string $string The string to truncate.
* @param int $limit The number of characters to truncate too.
* @param string $continuation The string to use to denote it was truncated.
* @param bool $isHtml Whether the string has HTML.
* @return string The truncated string.
*/
public function truncate(string $string, int $limit, string $continuation = '...', bool $isHtml = false): string;
StringHelper::increment()
/**
* Adds _1 to a string or increment the ending number to allow _2, _3, etc
*
* @param string $str String to increment.
* @param int $first Number that is used to mean first.
* @param string $separator Separator between the name and the number.
*/
public function increment(string $str, int $first = 1, string $separator = '_'): string;
StringHelper::startsWith()
/**
* Checks whether a string has a specific beginning.
*
* @param string $str String to check.
* @param string $start Beginning to check for.
* @param bool $ignoreCase Whether to ignore the case.
* @return bool whether a string starts with a specified beginning.
*/
public function startsWith(string $str, string $start, bool $ignoreCase = false): bool;
StringHelper::endsWith()
/**
* Checks whether a string has a specific ending.
*
* @param string $str String to check.
* @param string $end Ending to check for.
* @param bool $ignoreCase Whether to ignore the case.
* @return bool Whether a string ends with a specified ending.
*/
public function endsWith(string $str, string $end, bool $ignoreCase = false): bool;
StringHelper::random()
/**
* Creates a random string of characters
*
* @param string $type The type of string.
* @param int $length The number of characters.
* @return string|int|false The random string.
*/
public function random(string $type = 'alnum', int $length = 16): string|int|false;
StringHelper::alternator()
/**
* Returns a closure that will alternate between the args which to return.
* If you call the closure with false as the arg it will return the value without
* alternating the next time.
*
* @return Closure
*/
public function alternator(): Closure;
StringHelper::tr()
/**
* Parse the params from a string using strtr().
*
* @param string $string String to parse.
* @param array $array Params to str_replace.
*/
public function tr(mixed $string, array $array = []): mixed;
StringHelper::isJson()
/**
* Check if a string is json encoded.
*
* @param string $string String to check.
*/
public function isJson(string $string): bool;
StringHelper::isXml()
/**
* Check if a string is a valid XML.
*
* @param string $string String to check.
* @throws Exception
*/
public function isXml(string $string): bool;
StringHelper::isHtml()
/**
* Check if a string is html.
*
* @param string $string String to check.
*/
public function isHtml(string $string): bool;
StringHelper::strlen()
/**
* Find the position of the first occurrence of a substring in a string.
*
* @param string $str The string being measured for length.
* @param string|null $encoding Defaults to the setting in the config, which defaults to UTF-8.
* @return int|false The length of the string on success, and 0 if the string is empty.
*/
public function strlen(string $str, string|null $encoding = 'UTF-8'): int|false;
StringHelper::strpos()
/**
* Find position of first occurrence of string in a string.
*
* @param string $haystack The string being checked.
* @param mixed $needle The string to find in haystack.
* @param int $offset The search offset.
* @param string|null $encoding
* @return false|int Returns the position of where the needle exists relative to the beginning
* of the haystack string (independent of offset). Also note that string
* positions start at 0, and not 1.
* Returns false if the needle was not found.
*/
public function strpos(string $haystack, mixed $needle, int $offset = 0, string|null $encoding = 'UTF-8'): false|int;
StringHelper::strrpos()
/**
* Find position of last occurrence of a string in a string.
*
* @param string $haystack The string being checked.
* @param mixed $needle The string to find in haystack.
* @param int $offset The search offset.
* @return false|int Returns the numeric position of the last occurrence of needle in the
* haystack string. If needle is not found, it returns false.
*/
public function strrpos(string $haystack, mixed $needle, int $offset = 0): false|int;
StringHelper::substr()
/**
* Get part of string.
*
* @param string $str The string to extract the substring from.
* @param int $start If start is non-negative, the returned string will start at the start'th
* position in str, counting from zero. If start is negative, the returned
* string will start at the start'th character from the end of str.
* @param int|null $length Maximum number of characters to use from str. If omitted or NULL is passed,
* extract all characters to the end of the string.
* @return mixed Returns the extracted part of string; or false on failure, or an empty string.
*/
public function substr(string $str, int $start, int|null $length = null): mixed;
StringHelper::strtolower()
/**
* Make a string lowercase.
*
* @param string $str The string to convert to lowercase.
* @return string The lowercase string.
*/
public function strtolower(string $str): string;
StringHelper::strtoupper()
/**
* Make a string uppercase.
*
* @param string $str The string to convert to uppercase.
* @return string The uppercase string.
*/
public function strtoupper(string $str): string;
StringHelper::stripos()
/**
* Find the position of the first occurrence of a case-insensitive substring in a string.
*
* @param string $haystack The string from which to get the position of the last occurrence of needle.
* @param string $needle The string to find in haystack.
* @param int $offset The search offset.
* @return false|int Returns the position of where the needle exists relative to the beginning
* of the haystack string (independent of offset). Also note that string
* positions start at 0, and not 1.
* Returns false if the needle was not found.
*/
public function stripos(string $haystack, string $needle, int $offset = 0): bool|int;
StringHelper::strripos()
/**
* Finds position of last occurrence of a string within another, case insensitive.
*
* @param string $haystack The string from which to get the position of the last occurrence of needle.
* @param string $needle The string to find in haystack.
* @param int $offset The search offset.
* @return int|bool Returns the numeric position of the last occurrence of needle in the
* haystack string. If needle is not found, it returns false.
*/
public function strripos(string $haystack, string $needle, int $offset = 0): int|bool;
StringHelper::strstr()
/**
* Finds first occurrence of a string within another.
*
* @param string $haystack The string from which to get the position of the last occurrence of needle.
* @param string $needle The string to find in haystack.
* @param bool $beforeNeedle Determines which portion of haystack this function returns.
* @return string|bool The portion of haystack, or false if needle is not found.
*/
public function strstr(string $haystack, string $needle, bool $beforeNeedle = false): string|bool;
StringHelper::stristr()
/**
* Finds first occurrence of a string within another, case-insensitive.
*
* @param string $haystack The string from which to get the position of the last occurrence of needle.
* @param string $needle The string to find in haystack.
* @param bool $beforeNeedle Determines which portion of haystack this function returns.
* @return string|bool The portion of haystack, or false if needle is not found.
*/
public function stristr(string $haystack, string $needle, bool $beforeNeedle = false): string|bool;
StringHelper::strrchr()
/**
* Finds the last occurrence of a character in a string within another.
*
* @param string $haystack The string from which to get the last occurrence of needle.
* @param string $needle The string to find in haystack.
* @param bool $beforeNeedle
* @return false|string The portion of haystack, or false if needle is not found.
*/
public function strrchr(string $haystack, string $needle, bool $beforeNeedle = false): bool|string;
StringHelper::substrCount()
/**
* substr_count — Count the number of substring occurrences.
*
* @param string $haystack The string from which to get the position of the last occurrence of needle.
* @param string $needle The string to find in haystack.
* @param int $offset The search offset.
* @return int The number of occurrences found.
*/
public function substrCount(string $haystack, string $needle, int $offset = 0): int;
StringHelper::lcfirst()
/**
* Does not strtoupper first.
*
* @param string $str String to lowercase first letter.
*/
public function lcfirst(string $str): string;
StringHelper::ucfirst()
/**
* Does not strtolower first.
*
* @param string $str String to uppercase first letter.
*/
public function ucfirst(string $str): string;
StringHelper::ucwords()
/**
* First strtolower then ucwords.
*
* ucwords normally doesn't strtolower first
* but MB_CASE_TITLE does, so ucwords now too.
*
* @param string $str String to uppercase.
* @return string
*/
public function ucwords(string $str): string;