String Helpers

The String Helpers are lightweight functions for common text processing tasks, including truncation, extraction, slug generation, output escaping, random string creation, and content filtering to enhance security and consistency in your applications.

extract_content()

Extracts a substring from a given string, defined by start and end delimiters. The function searches for the first occurrence of the start delimiter and the first subsequent occurrence of the end delimiter, then returns the text found between them. If either delimiter is not found, or if they are in the wrong order, an empty string is returned.

filter_name()

Filters and sanitizes a name, typically used for usernames.

filter_str()

Filters and sanitizes a string, removing any disallowed HTML tags while preserving allowed ones.

get_last_part()

Retrieves the last part of a string based on a delimiter.

make_rand_str()

Generates a random string of characters, customizable by length and whether to use uppercase. Ideal for generating secure and unique keys or tokens.

nice_price()

Formats a number as a price with commas for thousands and optionally adds a currency symbol. If the formatted price is a whole number, decimals are removed.

out()

Provides a single, explicit, and security-focused way to safely output strings across multiple contexts. This function eliminates ambiguity, reduces developer error, and prevents cross-site scripting (XSS) vulnerabilities caused by incorrect escaping. Rather than remembering which escaping function to use in each scenario, you call out() and explicitly declare how the output will be used.

remove_substr_between()

Removes a portion of a string between two given substrings. If the optional $remove_all parameter is set to true, all matching portions will be removed.

remove_html_code()

Removes code sections from HTML content based on specified start and end patterns.

replace_html_tags()

Converts HTML content based on given specifications.

sanitize_filename()

Sanitizes a filename for safe storage and usage on the filesystem and web. This function removes or replaces special characters, handles international characters through transliteration, preserves file extensions, and prevents common security issues.

truncate_str()

Truncates a string to a specified maximum length. If the string's length exceeds the maximum, it is shortened and an ellipsis (...) is appended.

truncate_words()

Truncates a string to a specified maximum number of words. If the number of words in the string exceeds the maximum, the string is shortened to the maximum number of words and an ellipsis (...) is appended.

url_title()

Converts a string into a URL-friendly slug format. This function will transliterate the string to ASCII if the 'intl' extension is loaded and transliteration is set to true, converts any non-alphanumeric characters to dashes, trims them from the start and end, and converts everything to lowercase.