StringHelper
extends BaseStringHelper
in package
StringHelper.
Tags
Table of Contents
Methods
- base64UrlDecode() : string
- Decodes "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648).
- base64UrlEncode() : string
- Encodes string into "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648).
- basename() : string
- Returns the trailing name component of a path.
- byteLength() : int
- Returns the number of bytes in the given string.
- byteSubstr() : string
- Returns the portion of string specified by the start and length parameters.
- countWords() : int
- Counts words in a string.
- dirname() : string
- Returns parent directory's path.
- endsWith() : bool
- Check if given string ends with specified substring. Binary and multibyte safe.
- explode() : array<string|int, mixed>
- Explodes string into array, optionally trims values and skips empty ones.
- findBetween() : string|null
- Returns the portion of the string that lies between the first occurrence of the start string and the last occurrence of the end string after that.
- floatToString() : string
- Safely casts a float to string independent of the current locale.
- mask() : string
- Masks a portion of a string with a repeated character.
- matchWildcard() : bool
- Checks if the passed string would match the given shell wildcard pattern.
- mb_ucfirst() : string
- This method provides a unicode-safe implementation of built-in PHP function `ucfirst()`.
- mb_ucwords() : string
- This method provides a unicode-safe implementation of built-in PHP function `ucwords()`.
- normalizeNumber() : string
- Returns string representation of number value with replaced commas to dots, if decimal point of current locale is comma.
- startsWith() : bool
- Check if given string starts with specified substring. Binary and multibyte safe.
- truncate() : string
- Truncates a string to the number of characters specified.
- truncateWords() : string
- Truncates a string to the number of words specified.
- truncateHtml() : string
- Truncate a string while preserving the HTML.
Methods
base64UrlDecode()
Decodes "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648).
public
static base64UrlDecode(string $input) : string
Parameters
- $input : string
-
encoded string.
Tags
Return values
string —decoded string.
base64UrlEncode()
Encodes string into "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648).
public
static base64UrlEncode(string $input) : string
Note: Base 64 padding
=
may be at the end of the returned string.=
is not transparent to URL encoding.
Parameters
- $input : string
-
the string to encode.
Tags
Return values
string —encoded string.
basename()
Returns the trailing name component of a path.
public
static basename(string $path[, string $suffix = '' ]) : string
This method is similar to the php function basename()
except that it will
treat both \ and / as directory separators, independent of the operating system.
This method was mainly created to work on php namespaces. When working with real
file paths, php's basename()
should work fine for you.
Note: this method is not aware of the actual filesystem, or path components such as "..".
Parameters
- $path : string
-
A path string.
- $suffix : string = ''
-
If the name component ends in suffix this will also be cut off.
Tags
Return values
string —the trailing name component of the given path.
byteLength()
Returns the number of bytes in the given string.
public
static byteLength(string $string) : int
This method ensures the string is treated as a byte array by using mb_strlen()
.
Parameters
- $string : string
-
the string being measured for length
Return values
int —the number of bytes in the given string.
byteSubstr()
Returns the portion of string specified by the start and length parameters.
public
static byteSubstr(string $string, int $start[, int|null $length = null ]) : string
This method ensures the string is treated as a byte array by using mb_substr()
.
Parameters
- $string : string
-
the input string. Must be one character or longer.
- $start : int
-
the starting position
- $length : int|null = null
-
the desired portion length. If not specified or
null
, there will be no limit on length i.e. the output will be until the end of the string.
Tags
Return values
string —the extracted part of string, or FALSE on failure or an empty string.
countWords()
Counts words in a string.
public
static countWords(string $string) : int
Parameters
- $string : string
-
the text to calculate
Tags
Return values
intdirname()
Returns parent directory's path.
public
static dirname(string $path) : string
This method is similar to dirname()
except that it will treat
both \ and / as directory separators, independent of the operating system.
Parameters
- $path : string
-
A path string.
Tags
Return values
string —the parent directory's path.
endsWith()
Check if given string ends with specified substring. Binary and multibyte safe.
public
static endsWith(string $string, string $with[, bool $caseSensitive = true ]) : bool
Parameters
- $string : string
-
Input string to check
- $with : string
-
Part to search inside of the
$string
. - $caseSensitive : bool = true
-
Case sensitive search. Default is true. When case sensitive is enabled,
$with
must exactly match the ending of the string in order to get a true value.
Return values
bool —Returns true if first input ends with second input, false otherwise
explode()
Explodes string into array, optionally trims values and skips empty ones.
public
static explode(string $string[, string $delimiter = ',' ][, mixed $trim = true ][, bool $skipEmpty = false ]) : array<string|int, mixed>
Parameters
- $string : string
-
String to be exploded.
- $delimiter : string = ','
-
Delimiter. Default is ','.
- $trim : mixed = true
-
Whether to trim each element. Can be:
- boolean - to trim normally;
- string - custom characters to trim. Will be passed as a second argument to
trim()
function. - callable - will be called for each value instead of trim. Takes the only argument - value.
- $skipEmpty : bool = false
-
Whether to skip empty strings between delimiters. Default is false.
Tags
Return values
array<string|int, mixed>findBetween()
Returns the portion of the string that lies between the first occurrence of the start string and the last occurrence of the end string after that.
public
static findBetween(string $string, string $start, string $end) : string|null
Parameters
- $string : string
-
The input string.
- $start : string
-
The string marking the start of the portion to extract.
- $end : string
-
The string marking the end of the portion to extract.
Return values
string|null —The portion of the string between the first occurrence of start and the last occurrence of end, or null if either start or end cannot be found.
floatToString()
Safely casts a float to string independent of the current locale.
public
static floatToString(float|int $number) : string
The decimal separator will always be .
.
Parameters
- $number : float|int
-
a floating point number or integer.
Tags
Return values
string —the string representation of the number.
mask()
Masks a portion of a string with a repeated character.
public
static mask(string $string, int $start, int $length[, string $mask = '*' ]) : string
This method is multibyte-safe.
Parameters
- $string : string
-
The input string.
- $start : int
-
The starting position from where to begin masking. This can be a positive or negative integer. Positive values count from the beginning, negative values count from the end of the string.
- $length : int
-
The length of the section to be masked. The masking will start from the $start position and continue for $length characters.
- $mask : string = '*'
-
The character to use for masking. The default is '*'.
Return values
string —The masked string.
matchWildcard()
Checks if the passed string would match the given shell wildcard pattern.
public
static matchWildcard(string $pattern, string $string[, array<string|int, mixed> $options = [] ]) : bool
This function emulates [[fnmatch()]], which may be unavailable at certain environment, using PCRE.
Parameters
- $pattern : string
-
the shell wildcard pattern.
- $string : string
-
the tested string.
- $options : array<string|int, mixed> = []
-
options for matching. Valid options are:
- caseSensitive: bool, whether pattern should be case sensitive. Defaults to
true
. - escape: bool, whether backslash escaping is enabled. Defaults to
true
. - filePath: bool, whether slashes in string only matches slashes in the given pattern. Defaults to
false
.
- caseSensitive: bool, whether pattern should be case sensitive. Defaults to
Tags
Return values
bool —whether the string matches pattern or not.
mb_ucfirst()
This method provides a unicode-safe implementation of built-in PHP function `ucfirst()`.
public
static mb_ucfirst(string $string[, string $encoding = 'UTF-8' ]) : string
Parameters
- $string : string
-
the string to be proceeded
- $encoding : string = 'UTF-8'
-
Optional, defaults to "UTF-8"
Tags
Return values
stringmb_ucwords()
This method provides a unicode-safe implementation of built-in PHP function `ucwords()`.
public
static mb_ucwords(string $string[, string $encoding = 'UTF-8' ]) : string
Parameters
- $string : string
-
the string to be proceeded
- $encoding : string = 'UTF-8'
-
Optional, defaults to "UTF-8"
Tags
Return values
stringnormalizeNumber()
Returns string representation of number value with replaced commas to dots, if decimal point of current locale is comma.
public
static normalizeNumber(int|float|string $value) : string
Parameters
- $value : int|float|string
-
the value to normalize.
Tags
Return values
stringstartsWith()
Check if given string starts with specified substring. Binary and multibyte safe.
public
static startsWith(string $string, string $with[, bool $caseSensitive = true ]) : bool
Parameters
- $string : string
-
Input string
- $with : string
-
Part to search inside the $string
- $caseSensitive : bool = true
-
Case sensitive search. Default is true. When case sensitive is enabled,
$with
must exactly match the starting of the string in order to get a true value.
Return values
bool —Returns true if first input starts with second input, false otherwise
truncate()
Truncates a string to the number of characters specified.
public
static truncate(string $string, int $length[, string $suffix = '...' ][, string|null $encoding = null ][, bool $asHtml = false ]) : string
In order to truncate for an exact length, the $suffix char length must be counted towards the $length. For example
to have a string which is exactly 255 long with $suffix ...
of 3 chars, then StringHelper::truncate($string, 252, '...')
must be used to ensure you have 255 long string afterwards.
Parameters
- $string : string
-
The string to truncate.
- $length : int
-
How many characters from original string to include into truncated string.
- $suffix : string = '...'
-
String to append to the end of truncated string.
- $encoding : string|null = null
-
The charset to use, defaults to charset currently used by application.
- $asHtml : bool = false
-
Whether to treat the string being truncated as HTML and preserve proper HTML tags. This parameter is available since version 2.0.1.
Return values
string —the truncated string.
truncateWords()
Truncates a string to the number of words specified.
public
static truncateWords(string $string, int $count[, string $suffix = '...' ][, bool $asHtml = false ]) : string
Parameters
- $string : string
-
The string to truncate.
- $count : int
-
How many words from original string to include into truncated string.
- $suffix : string = '...'
-
String to append to the end of truncated string.
- $asHtml : bool = false
-
Whether to treat the string being truncated as HTML and preserve proper HTML tags. This parameter is available since version 2.0.1.
Return values
string —the truncated string.
truncateHtml()
Truncate a string while preserving the HTML.
protected
static truncateHtml(string $string, int $count, string $suffix[, string|bool $encoding = false ]) : string
Parameters
- $string : string
-
The string to truncate
- $count : int
-
The counter
- $suffix : string
-
String to append to the end of the truncated string.
- $encoding : string|bool = false
-
Encoding flag or charset.