BaseStringHelper
API reference › yii › helpers › BaseStringHelper
1 subclass
BaseStringHelper provides concrete implementation for StringHelper.
Do not use BaseStringHelper. Use StringHelper instead.
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
base64UrlDecode() static | Decodes “Base 64 Encoding with URL and Filename Safe Alphabet” (RFC 4648). | BaseStringHelper |
base64UrlEncode() static | Encodes string into “Base 64 Encoding with URL and Filename Safe Alphabet” (RFC 4648). | BaseStringHelper |
basename() static | Returns the trailing name component of a path. | BaseStringHelper |
byteLength() static | Returns the number of bytes in the given string. | BaseStringHelper |
byteSubstr() static | Returns the portion of string specified by the start and length parameters. | BaseStringHelper |
convertIniSizeToBytes() static | Converts php.ini style size to bytes. | BaseStringHelper |
countWords() static | Counts words in a string. | BaseStringHelper |
dirname() static | Returns parent directory’s path. | BaseStringHelper |
endsWith() static | Check if given string ends with specified substring. Binary and multibyte safe. | BaseStringHelper |
explode() static | Explodes string into array, optionally trims values and skips empty ones. | BaseStringHelper |
findBetween() static | 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. | BaseStringHelper |
floatToString() static | Safely casts a float to string independent of the current locale. | BaseStringHelper |
mask() static | Masks a portion of a string with a repeated character. | BaseStringHelper |
matchWildcard() static | Checks if the passed string would match the given shell wildcard pattern. | BaseStringHelper |
mb_ucfirst() static | This method provides a unicode-safe implementation of built-in PHP function ucfirst(). | BaseStringHelper |
mb_ucwords() static | This method provides a unicode-safe implementation of built-in PHP function ucwords(). | BaseStringHelper |
normalizeNumber() static | Returns string representation of number value with replaced commas to dots, if decimal point of current locale is comma. | BaseStringHelper |
startsWith() static | Check if given string starts with specified substring. Binary and multibyte safe. | BaseStringHelper |
truncate() static | Truncates a string to the number of characters specified. | BaseStringHelper |
truncateWords() static | Truncates a string to the number of words specified. | BaseStringHelper |
truncateHtml() protected static | Truncate a string while preserving the HTML. | BaseStringHelper |
Method details
Section titled “Method details”base64UrlDecode()
Section titled “base64UrlDecode() ”public static function base64UrlDecode(string $input): stringDecodes “Base 64 Encoding with URL and Filename Safe Alphabet” (RFC 4648).
Parameters
| Parameter | Type | Description |
|---|---|---|
$input | string | Encoded string. |
Returns
string: Decoded string.
Since
2.0.12
See also
base64UrlEncode()
Section titled “base64UrlEncode() ”public static function base64UrlEncode(string $input): stringEncodes string into “Base 64 Encoding with URL and Filename Safe Alphabet” (RFC 4648).
Note: Base 64 padding
=may be at the end of the returned string.=is not transparent to URL encoding.
Parameters
| Parameter | Type | Description |
|---|---|---|
$input | string | The string to encode. |
Returns
string: Encoded string.
Since
2.0.12
See also
basename()
Section titled “basename() ”public static function basename(string $path, string $suffix = ''): stringReturns the trailing name component of a path.
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
| Parameter | Type | Description |
|---|---|---|
$path | string | A path string. |
$suffix | string | If the name component ends in suffix this will also be cut off. |
Returns
string: The trailing name component of the given path.
See also
byteLength()
Section titled “byteLength() ”public static function byteLength(string $string): intReturns the number of bytes in the given string.
This method ensures the string is treated as a byte array by using mb_strlen().
Parameters
| Parameter | Type | Description |
|---|---|---|
$string | string | The string being measured for length |
Returns
int: The number of bytes in the given string.
byteSubstr()
Section titled “byteSubstr() ”public static function byteSubstr(string $string, int $start, int|null $length = null): stringReturns the portion of string specified by the start and length parameters.
This method ensures the string is treated as a byte array by using mb_substr().
Parameters
| Parameter | Type | Description |
|---|---|---|
$string | string | The input string. Must be one character or longer. |
$start | int | The starting position |
$length | int| | 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. |
Returns
string: The extracted part of string, or FALSE on failure or an empty string.
See also
convertIniSizeToBytes()
Section titled “convertIniSizeToBytes() ”public static function convertIniSizeToBytes(string $string): intConverts php.ini style size to bytes.
Parameters
| Parameter | Type | Description |
|---|---|---|
$string | string | Php.ini style size. Examples: 512M, 1024K, 1G, 256. |
Returns
int: The number of bytes equivalent to the specified string.
Since
2.0.54
countWords()
Section titled “countWords() ”public static function countWords(string $string): intCounts words in a string.
Parameters
| Parameter | Type | Description |
|---|---|---|
$string | string | The text to calculate |
Returns
int
Since
2.0.8
dirname()
Section titled “dirname() ”public static function dirname(string $path): stringReturns parent directory’s path.
This method is similar to dirname() except that it will treat
both \ and / as directory separators, independent of the operating system.
Parameters
| Parameter | Type | Description |
|---|---|---|
$path | string | A path string. |
Returns
string: The parent directory’s path.
See also
endsWith()
Section titled “endsWith() ”public static function endsWith(string $string, string $with, bool $caseSensitive = true): boolCheck if given string ends with specified substring. Binary and multibyte safe.
Parameters
| Parameter | Type | Description |
|---|---|---|
$string | string | Input string to check |
$with | string | Part to search inside of the $string. |
$caseSensitive | bool | 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. |
Returns
bool: Returns true if first input ends with second input, false otherwise
explode()
Section titled “explode() ”public static function explode(string $string, string $delimiter = ',', mixed $trim = true, bool $skipEmpty = false): arrayExplodes string into array, optionally trims values and skips empty ones.
Parameters
| Parameter | Type | Description |
|---|---|---|
$string | string | String to be exploded. |
$delimiter | string | Delimiter. Default is ’,’. |
$trim | mixed | 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 | Whether to skip empty strings between delimiters. Default is false. |
Returns
array
Since
2.0.4
findBetween()
Section titled “findBetween() ”public static function findBetween(string $string, string $start, string $end): string|nullReturns 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.
Parameters
| Parameter | Type | Description |
|---|---|---|
$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. |
Returns
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()
Section titled “floatToString() ”public static function floatToString(float|int $number): stringSafely casts a float to string independent of the current locale.
The decimal separator will always be ..
Parameters
| Parameter | Type | Description |
|---|---|---|
$number | float| | A floating point number or integer. |
Returns
string: The string representation of the number.
Since
2.0.13
mask()
Section titled “mask() ”public static function mask(string $string, int $start, int $length, string $mask = '*'): stringMasks a portion of a string with a repeated character.
This method is multibyte-safe.
Parameters
| Parameter | Type | Description |
|---|---|---|
$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 ’*’. |
Returns
string: The masked string.
matchWildcard()
Section titled “matchWildcard() ”public static function matchWildcard(string $pattern, string $string, array $options = []): boolChecks if the passed string would match the given shell wildcard pattern.
This function emulates fnmatch(), which may be unavailable at certain environment, using PCRE.
Parameters
| Parameter | Type | Description |
|---|---|---|
$pattern | string | The shell wildcard pattern. |
$string | string | The tested string. |
$options | array | 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. |
Returns
bool: Whether the string matches pattern or not.
Since
2.0.14
mb_ucfirst()
Section titled “mb_ucfirst() ”public static function mb_ucfirst(string $string, string $encoding = 'UTF-8'): stringThis method provides a unicode-safe implementation of built-in PHP function ucfirst().
Parameters
| Parameter | Type | Description |
|---|---|---|
$string | string | The string to be proceeded |
$encoding | string | Optional, defaults to “UTF-8” |
Returns
string
Since
2.0.16
See also
mb_ucwords()
Section titled “mb_ucwords() ”public static function mb_ucwords(string $string, string $encoding = 'UTF-8'): stringThis method provides a unicode-safe implementation of built-in PHP function ucwords().
Parameters
| Parameter | Type | Description |
|---|---|---|
$string | string | The string to be proceeded |
$encoding | string | Optional, defaults to “UTF-8” |
Returns
string
Since
2.0.16
See also
normalizeNumber()
Section titled “normalizeNumber() ”public static function normalizeNumber(int|float|string $value): stringReturns string representation of number value with replaced commas to dots, if decimal point of current locale is comma.
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | int| | The value to normalize. |
Returns
string
Since
2.0.11
startsWith()
Section titled “startsWith() ”public static function startsWith(string $string, string $with, bool $caseSensitive = true): boolCheck if given string starts with specified substring. Binary and multibyte safe.
Parameters
| Parameter | Type | Description |
|---|---|---|
$string | string | Input string |
$with | string | Part to search inside the $string |
$caseSensitive | bool | 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. |
Returns
bool: Returns true if first input starts with second input, false otherwise
truncate()
Section titled “truncate() ”public static function truncate(string $string, int $length, string $suffix = '...', string|null $encoding = null, bool $asHtml = false): stringTruncates a string to the number of characters specified.
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
| Parameter | Type | Description |
|---|---|---|
$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| | The charset to use, defaults to charset currently used by application. |
$asHtml | bool | Whether to treat the string being truncated as HTML and preserve proper HTML tags. This parameter is available since version 2.0.1. |
Returns
string: The truncated string.
truncateWords()
Section titled “truncateWords() ”public static function truncateWords(string $string, int $count, string $suffix = '...', bool $asHtml = false): stringTruncates a string to the number of words specified.
Parameters
| Parameter | Type | Description |
|---|---|---|
$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 | Whether to treat the string being truncated as HTML and preserve proper HTML tags. This parameter is available since version 2.0.1. |
Returns
string: The truncated string.
truncateHtml()
Section titled “truncateHtml() ”protected static function truncateHtml(string $string, int $count, string $suffix, string|bool $encoding = false): stringTruncate a string while preserving the HTML.
Parameters
| Parameter | Type | Description |
|---|---|---|
$string | string | The string to truncate |
$count | int | The counter |
$suffix | string | String to append to the end of the truncated string. |
$encoding | string| | Encoding flag or charset. |
Returns
string
Since
2.0.1