StringHelper
in package
String helper
Table of Contents
Methods
- AddNoIndex() : string
- Adds a "noindex" directive to the given text.
- AddToQueryString() : string
- Adds query parameters to a URL.
- CreateHID() : string
- Creates a human-readable identifier (HID) from a given text.
- EndsWith() : bool
- Checks if a string ends with a specified suffix.
- Expand() : string
- Expands a string by repeating a character to a specified length.
- Explode() : array<string|int, mixed>|null
- Splits a string or array into an array of substrings using specified delimiters.
- FormatFileSize() : string
- Formats a file size value into a human-readable string.
- FormatSequence() : string
- Formats a sequence value with appropriate labels (e.g., years, months, etc.).
- FromCamelCaseAttr() : string
- Converts a camelCase attribute string to a hyphen-separated format.
- FromCamelCaseVar() : string
- Converts a camel-cased string to a regular variable name.
- GUID() : string
- Generates a globally unique identifier (GUID).
- Implode() : string
- Joins array elements into a single string using a specified delimiter.
- ImplodeWithKeys() : string
- Joins array keys and values into a single string using specified delimiters.
- IsEmail() : bool
- Checks if a string represents a valid email address.
- IsJsonString() : bool
- IsLower() : bool
- Checks whether a given string consists entirely of lowercase letters.
- IsUpper() : bool
- Checks whether a given string consists entirely of uppercase letters.
- IsUrl() : bool
- Checks if a string represents a valid URL.
- Length() : int
- Calculates the length (number of characters) of a given string.
- ParseAsUrl() : ExtendedObject
- Parses a string as a URL and returns an ExtendedObject with relevant components.
- PrepareAttribute() : string
- Prepares an attribute string for use in HTML or other contexts.
- Randomize() : string
- Generates a random string of a specified length.
- RemoveEmoji() : string
- Removes emojis from the given text.
- Replace() : string|array<string|int, mixed>|bool
- Replaces occurrences of a search string or an array of search strings with a replacement string in the given subject string or array.
- StartsWith() : bool
- Checks if a string starts with a specified prefix.
- StripHTML() : string
- Strips HTML tags from a given string.
- StripHtmlAndBody() : string
- Removes the <html> and <body> tags from the given HTML string.
- Substring() : string
- Extracts a substring from a given string.
- ToCamelCaseAttr() : string
- Converts a string to camelCase attribute format.
- ToCamelCaseVar() : string
- Converts a string to camel case from underscore_case string
- ToLower() : string
- Converts a given string to lowercase.
- ToObject() : ExtendedObject
- Converts a string into an ExtendedObject using specified splitters.
- ToUpper() : string
- Converts a given string to uppercase.
- ToUpperFirst() : string
- Converts the first character of a given string to uppercase.
- Transliterate() : string
- Transliterates a string to a different character encoding or script.
- TransliterateBack() : string
- Transliterates a string back to its original form from a different character encoding or script.
- Trim() : string
- Trims specified characters from the beginning and end of a string.
- TrimLength() : string|null
- Trims a string to a specified length and adds an ellipsis if needed.
- Unescape() : string
- Unescapes special characters in a string.
- UniqueWords() : array<string|int, mixed>
- Retrieves an array of unique words from a given string.
- UrlToNamespace() : string
- Converts a URL to a corresponding namespace.
- Words() : string
- Retrieves the first N words from a given text.
Methods
AddNoIndex()
Adds a "noindex" directive to the given text.
public
static AddNoIndex(string $text[, bool $hard = true ][, string $domain = '' ]) : string
Parameters
- $text : string
-
The input text to modify.
- $hard : bool = true
-
Whether to apply a "hard" noindex (default: true).
- $domain : string = ''
-
Optional. The domain to associate with the noindex directive.
Return values
string —The modified text with the "noindex" directive.
AddToQueryString()
Adds query parameters to a URL.
public
static AddToQueryString(string $url, string|array<string|int, mixed>|object $params[, bool $encode = true ]) : string
Parameters
- $url : string
-
The base URL.
- $params : string|array<string|int, mixed>|object
-
The query parameters to add (can be a string, array, or object).
- $encode : bool = true
-
Whether to URL-encode the parameters (default: true).
Return values
string —The modified URL with added query parameters.
CreateHID()
Creates a human-readable identifier (HID) from a given text.
public
static CreateHID(string $text[, bool $trans = true ]) : string
Parameters
- $text : string
-
The input text to generate the HID from.
- $trans : bool = true
-
Whether to transliterate the text (default: true).
Return values
string —The human-readable identifier.
EndsWith()
Checks if a string ends with a specified suffix.
public
static EndsWith(string $string, string $end) : bool
Parameters
- $string : string
-
The input string to check.
- $end : string
-
The suffix to compare.
Return values
bool —True if the string ends with the specified suffix, false otherwise.
Expand()
Expands a string by repeating a character to a specified length.
public
static Expand(string $s, int $l, string $c) : string
Parameters
- $s : string
-
The input string to expand.
- $l : int
-
The desired length of the expanded string.
- $c : string
-
The character to repeat for expansion.
Return values
string —The expanded string.
Explode()
Splits a string or array into an array of substrings using specified delimiters.
public
static Explode(string|array<string|int, mixed> $string, string|array<string|int, mixed> $delimiters[, bool $addDelimiters = false ]) : array<string|int, mixed>|null
Parameters
- $string : string|array<string|int, mixed>
-
The input string or array to split.
- $delimiters : string|array<string|int, mixed>
-
The delimiter(s) used for splitting (can be a string or an array of strings).
- $addDelimiters : bool = false
-
Whether to include the delimiters in the resulting array (default: false).
Return values
array<string|int, mixed>|null —An array of substrings obtained by splitting the input string or array.
FormatFileSize()
Formats a file size value into a human-readable string.
public
static FormatFileSize(int $number[, int $range = 1024 ][, array<string|int, mixed> $postfixes = ["bytes", "Kb", "Mb", "Gb", "Tb"] ]) : string
Parameters
- $number : int
-
The file size in bytes.
- $range : int = 1024
-
Optional. The base range for formatting (default: 1024).
- $postfixes : array<string|int, mixed> = ["bytes", "Kb", "Mb", "Gb", "Tb"]
-
Optional. An array of postfixes for different size units (e.g., ["bytes", "Kb", "Mb", "Gb", "Tb"]).
Return values
string —The formatted file size string.
FormatSequence()
Formats a sequence value with appropriate labels (e.g., years, months, etc.).
public
static FormatSequence(float $secuence[, array<string|int, mixed> $labels = ["год", "года", "лет"] ][, bool $viewnumber = false ]) : string
Parameters
- $secuence : float
- $labels : array<string|int, mixed> = ["год", "года", "лет"]
-
An array of labels for different parts of the sequence (e.g., ["год", "года", "лет"]).
- $viewnumber : bool = false
-
Whether to include the numeric value in the output (default: false).
Return values
string —The formatted sequence with labels.
FromCamelCaseAttr()
Converts a camelCase attribute string to a hyphen-separated format.
public
static FromCamelCaseAttr(string $str[, string $splitter = '-' ][, bool $forceLowerCase = true ]) : string
Parameters
- $str : string
-
The input string in camelCase format.
- $splitter : string = '-'
-
The character used to separate words (optional, default is '-').
- $forceLowerCase : bool = true
-
Whether to force the output to be in lowercase (optional, default is true).
Return values
string —The converted string in hyphen-separated attribute format.
FromCamelCaseVar()
Converts a camel-cased string to a regular variable name.
public
static FromCamelCaseVar(string $str) : string
Parameters
- $str : string
-
The input string in camel case.
Return values
string —The converted variable name.
GUID()
Generates a globally unique identifier (GUID).
public
static GUID([bool $showSeparator = true ]) : string
Parameters
- $showSeparator : bool = true
-
Whether to include hyphens as separators (default: true).
Return values
string —The generated GUID.
Implode()
Joins array elements into a single string using a specified delimiter.
public
static Implode(array<string|int, mixed> $array, string $splitter) : string
Parameters
- $array : array<string|int, mixed>
-
The array of strings to implode.
- $splitter : string
-
The delimiter used to join the elements.
Return values
string —The resulting string after joining the array elements.
ImplodeWithKeys()
Joins array keys and values into a single string using specified delimiters.
public
static ImplodeWithKeys(array<string|int, mixed> $array, string $splitter1, string $splitter2[, string $keyDecorator = '' ]) : string
Parameters
- $array : array<string|int, mixed>
-
The associative array to implode.
- $splitter1 : string
-
The delimiter used between keys and values.
- $splitter2 : string
-
The delimiter used between key-value pairs.
- $keyDecorator : string = ''
-
Optional. A decorator for array keys (e.g., prefix, suffix).
Return values
string —The resulting string after joining keys and values.
IsEmail()
Checks if a string represents a valid email address.
public
static IsEmail(string $address[, bool $checkThatDomainExists = false ]) : bool
Parameters
- $address : string
-
The email address to validate.
- $checkThatDomainExists : bool = false
-
Whether to verify that the domain exists (default: false).
Return values
bool —True if the email address is valid, false otherwise.
IsJsonString()
public
static IsJsonString(string $s) : bool
Parameters
- $s : string
Return values
boolIsLower()
Checks whether a given string consists entirely of lowercase letters.
public
static IsLower(string $s) : bool
Parameters
- $s : string
-
The input string to check.
Return values
bool —True if the string contains only uppercase letters, false otherwise.
IsUpper()
Checks whether a given string consists entirely of uppercase letters.
public
static IsUpper(string $s) : bool
Parameters
- $s : string
-
The input string to check.
Return values
bool —True if the string contains only uppercase letters, false otherwise.
IsUrl()
Checks if a string represents a valid URL.
public
static IsUrl(string $address) : bool
Parameters
- $address : string
-
The URL to validate.
Return values
bool —True if the URL is valid, false otherwise.
Length()
Calculates the length (number of characters) of a given string.
public
static Length(string $string) : int
Parameters
- $string : string
-
The input string to measure.
Return values
int —The length of the input string.
ParseAsUrl()
Parses a string as a URL and returns an ExtendedObject with relevant components.
public
static ParseAsUrl(string $url) : ExtendedObject
Parameters
- $url : string
-
The input URL to parse.
Return values
ExtendedObject —An object containing components like scheme, host, path, query, etc.
PrepareAttribute()
Prepares an attribute string for use in HTML or other contexts.
public
static PrepareAttribute(string $string[, bool $quoters = false ]) : string
Parameters
- $string : string
-
The input attribute string.
- $quoters : bool = false
-
Whether to add quotes around the attribute value (default: false).
Return values
string —The prepared attribute string.
Randomize()
Generates a random string of a specified length.
public
static Randomize(int $length) : string
Parameters
- $length : int
-
The desired length of the random string.
Return values
string —A randomly generated string.
RemoveEmoji()
Removes emojis from the given text.
public
static RemoveEmoji(string $text) : string
Parameters
- $text : string
-
The input text containing emojis.
Return values
string —The text with emojis removed.
Replace()
Replaces occurrences of a search string or an array of search strings with a replacement string in the given subject string or array.
public
static Replace(string|array<string|int, mixed> $subject, string|array<string|int, mixed> $search, string|array<string|int, mixed> $replace[, int &$count = 0 ]) : string|array<string|int, mixed>|bool
Parameters
- $subject : string|array<string|int, mixed>
-
The input string or array to search and replace within.
- $search : string|array<string|int, mixed>
-
The search string or an array of search strings.
- $replace : string|array<string|int, mixed>
-
The replacement string or an array of replacement strings.
- $count : int = 0
-
(Optional) A variable to store the number of replacements made (default is 0).
Return values
string|array<string|int, mixed>|bool —The modified string or array after replacements, or false on failure.
StartsWith()
Checks if a string starts with a specified prefix.
public
static StartsWith(string $string, string $start) : bool
Parameters
- $string : string
-
The input string to check.
- $start : string
-
The prefix to compare.
Return values
bool —True if the string starts with the specified prefix, false otherwise.
StripHTML()
Strips HTML tags from a given string.
public
static StripHTML(string $html[, string|null $allowedTags = null ]) : string
Parameters
- $html : string
-
The input string containing HTML.
- $allowedTags : string|null = null
-
Optional. A list of allowed HTML tags (e.g., "
Return values
string —The string with HTML tags removed.
StripHtmlAndBody()
Removes the <html> and <body> tags from the given HTML string.
public
static StripHtmlAndBody(string $html) : string
Parameters
- $html : string
-
The input HTML string.
Return values
string —The modified HTML string with the
tag and its content removed.Substring()
Extracts a substring from a given string.
public
static Substring(string $string, int $start[, int|null $length = null ]) : string
Parameters
- $string : string
-
The input string.
- $start : int
-
The starting position (index) from which to extract the substring.
- $length : int|null = null
-
Optional. The length of the substring to extract (default: until the end of the string).
Return values
string —The extracted substring.
ToCamelCaseAttr()
Converts a string to camelCase attribute format.
public
static ToCamelCaseAttr(string $str[, bool $firstCapital = false ][, string $splitter = '\-' ]) : string
Parameters
- $str : string
-
The input string to convert.
- $firstCapital : bool = false
-
Whether the first letter should be capitalized (optional, default is false).
- $splitter : string = '\-'
-
The character used to split words (optional, default is '-').
Return values
string —The converted string in camelCase attribute format.
ToCamelCaseVar()
Converts a string to camel case from underscore_case string
public
static ToCamelCaseVar(string $str[, bool $firstCapital = false ]) : string
Parameters
- $str : string
-
The input string to convert.
- $firstCapital : bool = false
-
Whether the first letter should be capitalized (default: false).
Return values
string —The camel-cased version of the input string.
ToLower()
Converts a given string to lowercase.
public
static ToLower(string $s) : string
Parameters
- $s : string
-
The input string to convert.
Return values
string —The lowercase version of the input string.
ToObject()
Converts a string into an ExtendedObject using specified splitters.
public
static ToObject(string $string[, array<string|int, mixed> $splitters = ['&', '='] ]) : ExtendedObject
Parameters
- $string : string
-
The input string to convert.
- $splitters : array<string|int, mixed> = ['&', '=']
-
An array of characters to split the string (default: ['&', '=']).
Return values
ExtendedObject —The resulting ExtendedObject.
ToUpper()
Converts a given string to uppercase.
public
static ToUpper(string $s) : string
Parameters
- $s : string
-
The input string to convert.
Return values
string —The lowercase version of the input string.
ToUpperFirst()
Converts the first character of a given string to uppercase.
public
static ToUpperFirst(string $str) : string
Parameters
- $str : string
-
The input string.
Return values
string —The string with the first character in uppercase.
Transliterate()
Transliterates a string to a different character encoding or script.
public
static Transliterate(string $string) : string
Parameters
- $string : string
-
The input string to transliterate.
Return values
string —The transliterated string.
TransliterateBack()
Transliterates a string back to its original form from a different character encoding or script.
public
static TransliterateBack(string $string) : string
Parameters
- $string : string
-
The input string to reverse transliterate.
Return values
string —The original string before transliteration.
Trim()
Trims specified characters from the beginning and end of a string.
public
static Trim(string $string[, string $trim_chars = '\s' ]) : string
Parameters
- $string : string
-
The input string to trim.
- $trim_chars : string = '\s'
-
Characters to remove (default: '\s' which includes whitespace).
Return values
string —The trimmed string.
TrimLength()
Trims a string to a specified length and adds an ellipsis if needed.
public
static TrimLength(string $str, int $length[, string $ellipsis = "..." ]) : string|null
Parameters
- $str : string
-
The input string to trim.
- $length : int
-
The maximum length of the trimmed string.
- $ellipsis : string = "..."
-
Optional. The ellipsis to add when truncating (default: "...").
Return values
string|null —The trimmed string or null if the input string is empty.
Unescape()
Unescapes special characters in a string.
public
static Unescape(string $s) : string
Parameters
- $s : string
-
The input string to unescape.
Return values
string —The unescaped string.
UniqueWords()
Retrieves an array of unique words from a given string.
public
static UniqueWords(string $string[, int $minlen = 3 ]) : array<string|int, mixed>
Parameters
- $string : string
-
The input string to analyze.
- $minlen : int = 3
-
Optional. The minimum length of words to consider (default: 3).
Return values
array<string|int, mixed> —An array containing unique words from the input string.
UrlToNamespace()
Converts a URL to a corresponding namespace.
public
static UrlToNamespace(string $url) : string
Parameters
- $url : string
-
The input URL to convert.
Return values
string —The namespace derived from the URL.
Words()
Retrieves the first N words from a given text.
public
static Words(string $text, int $n[, string $ellipsis = "..." ]) : string
Parameters
- $text : string
-
The input text.
- $n : int
-
The number of words to extract.
- $ellipsis : string = "..."
-
Optional. The ellipsis to add if the text is truncated (default: "...").
Return values
string —The extracted words.