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, returning the text found between them. If either delimiter is not found, or if they appear in the wrong order, an empty string is returned.

filter_name()

Filters and sanitizes a name string by removing characters that are not typically valid in personal or business names. An optional array of additional allowed characters can be specified to accommodate edge cases.

filter_str()

Filters and sanitizes a string by removing potentially harmful content while optionally preserving specified HTML tags. This is useful for processing user input that may contain limited HTML formatting.

filter_string()

An alias for filter_str() retained for backward compatibility. Filters and sanitizes a string by removing potentially harmful content while optionally preserving specified HTML tags.

get_last_part()

Retrieves the last part of a string after splitting it on a given delimiter. This is useful for extracting slugs, IDs, or file extensions from structured strings.

make_rand_str()

Generates a cryptographically secure random string of alphanumeric characters. This is useful for generating tokens, passwords, API keys, and other identifiers.

nice_price()

Formats a number as a price with commas for thousands separators and an optional currency symbol. This function is useful for displaying monetary values in a human-readable format.

out()

Safely escapes and formats a string for various output contexts. This function is essential for preventing XSS attacks by ensuring that user-supplied data is properly encoded before being rendered in HTML, XML, JSON, JavaScript, or HTML attributes. Null values are converted to empty strings.

remove_html_code()

Removes code sections from HTML content based on specified opening and closing patterns. This is useful for stripping out unwanted markup, comments, or embedded scripts from HTML strings.

remove_substr_between()

Removes a portion of a string between two given substrings. Optionally, all matching portions can be removed rather than just the first occurrence.

remove_html_code()

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

replace_html_tags()

Converts HTML content by replacing specified tag patterns with new tags. The specifications array defines the opening and closing strings to find, and what to replace them with.

sanitize_filename()

Sanitizes a filename for safe storage and usage. This function transliterates international characters, removes or replaces special characters and whitespace, preserves file extensions, prevents null byte attacks, generates fallback names for edge cases, and limits filename length to prevent filesystem issues.

truncate_str()

Truncates a string to a specified maximum length. An ellipsis (...) is appended to indicate that the string has been shortened.

truncate_words()

Truncates a string to a specified maximum number of words. An ellipsis (...) is appended to indicate that the string has been shortened. This is useful for creating excerpts or previews of longer content.

url_title()

Converts a string into a URL-friendly slug format. If the intl extension is loaded and transliteration is enabled, international characters are converted to their ASCII equivalents. Non-alphanumeric characters are replaced with dashes, which are trimmed from the start and end, and the result is lowercased.