Text Helper
The Text
class provides various helper methods for manipulating and formatting text.
Namespace: Medialight\Textstem\Helpers
toHtml
Return formatted HTML paragraph string with escaped special characters.
public static function toHtml($string): string
Parameters:
$string
(string): The string to convert to HTML.
Returns:
- (string): The formatted HTML paragraph string.
strip_bare
Strip out all formatting from some HTML.
public static function strip_bare($str, $treatasHTML = false): string
Parameters:
$str
(string): The HTML string to strip formatting from.$treatasHTML
(bool): Whether to treat the input as HTML. Default is false.
Returns:
- (string): The stripped string without formatting.
formatForSort
Format a string for sorting. (eg 'The Great Escape' => 'Great Escape, The')
public static function formatForSort(string $rawString): string
Parameters:
$rawString
(string): The original string to be formatted.
Returns:
- (string): The formatted string for sorting.
formatFromSort
Format a string from sorting. (eg 'Great Escape, The' => 'The Great Escape')
public static function formatFromSort(string $str): string
Parameters:
$str
(string): The sorted string to be formatted.
Returns:
- (string): The formatted string.
nicefy
Convert a string to a more readable format by handling hyphens, slashes, and camelCase.
public static function nicefy($str, $ucwrds = true, $keepHyphens = true): string
Parameters:
$str
(string): The string to be formatted.$ucwrds
(bool): Whether to convert the result to title case. Default is true.$keepHyphens
(bool): Whether to keep hyphens in the string. Default is true.
Returns:
- (string): The formatted string.
toArray
This function is the same as explode, but trims white space and skips empties.
public static function toArray(mixed $str = '', ?string $sep = ','): array
Parameters:
$str
(mixed): The string to be exploded or an array to be returned as is.$sep
(string|null): The separator to use for exploding the string. Default is comma.
Returns:
- (array): The resulting array after exploding and filtering.
truncate
Truncate a given text, preserving word boundaries and adding a signify chop with.
public static function truncate(mixed $text = '', ?int $length = 50, $signifyChopWith = '...'): string
Parameters:
$text
(mixed): The text to be truncated. Default is an empty string.$length
(int|null): The maximum length of the truncated text. Default is 50.$signifyChopWith
(string): The signify chop with to be added at the end of the truncated text. Default is '...'.
Returns:
- (string): The truncated text with signify chop with added.
truncateMiddle
Truncates a given text in the middle if it exceeds the specified length.
public static function truncateMiddle(mixed $text = '', ?int $length = 50, $signifyChopWith = '...'): string
Parameters:
$text
(mixed): The text to truncate. Default is an empty string.$length
(int|null): The maximum length of the truncated text. Default is 50.$signifyChopWith
(string): The string to insert at the truncation point. Default is '...'.
Returns:
- (string): The truncated text, if necessary.