Skip to content
PHP Ecosystem
Explore

BaseStringHelper

API reference › yii › helpers › BaseStringHelper

classyii\helpers\BaseStringHelper
Since2.0
Sourceframework/helpers/BaseStringHelper.php
1 subclass

BaseStringHelper provides concrete implementation for StringHelper.

Do not use BaseStringHelper. Use StringHelper instead.

MethodDescriptionDefined by
base64UrlDecode() staticDecodes “Base 64 Encoding with URL and Filename Safe Alphabet” (RFC 4648).BaseStringHelper
base64UrlEncode() staticEncodes string into “Base 64 Encoding with URL and Filename Safe Alphabet” (RFC 4648).BaseStringHelper
basename() staticReturns the trailing name component of a path.BaseStringHelper
byteLength() staticReturns the number of bytes in the given string.BaseStringHelper
byteSubstr() staticReturns the portion of string specified by the start and length parameters.BaseStringHelper
convertIniSizeToBytes() staticConverts php.ini style size to bytes.BaseStringHelper
countWords() staticCounts words in a string.BaseStringHelper
dirname() staticReturns parent directory’s path.BaseStringHelper
endsWith() staticCheck if given string ends with specified substring. Binary and multibyte safe.BaseStringHelper
explode() staticExplodes string into array, optionally trims values and skips empty ones.BaseStringHelper
findBetween() staticReturns 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() staticSafely casts a float to string independent of the current locale.BaseStringHelper
mask() staticMasks a portion of a string with a repeated character.BaseStringHelper
matchWildcard() staticChecks if the passed string would match the given shell wildcard pattern.BaseStringHelper
mb_ucfirst() staticThis method provides a unicode-safe implementation of built-in PHP function ucfirst().BaseStringHelper
mb_ucwords() staticThis method provides a unicode-safe implementation of built-in PHP function ucwords().BaseStringHelper
normalizeNumber() staticReturns string representation of number value with replaced commas to dots, if decimal point of current locale is comma.BaseStringHelper
startsWith() staticCheck if given string starts with specified substring. Binary and multibyte safe.BaseStringHelper
truncate() staticTruncates a string to the number of characters specified.BaseStringHelper
truncateWords() staticTruncates a string to the number of words specified.BaseStringHelper
truncateHtml() protected staticTruncate a string while preserving the HTML.BaseStringHelper
public static function base64UrlDecode(string $input): string

Decodes “Base 64 Encoding with URL and Filename Safe Alphabet” (RFC 4648).

Parameters

ParameterTypeDescription
$inputstringEncoded string.

Returns

string: Decoded string.

Since

2.0.12

See also

public static function base64UrlEncode(string $input): string

Encodes 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

ParameterTypeDescription
$inputstringThe string to encode.

Returns

string: Encoded string.

Since

2.0.12

See also

public static function basename(string $path, string $suffix = ''): string

Returns 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

ParameterTypeDescription
$pathstringA path string.
$suffixstringIf the name component ends in suffix this will also be cut off.

Returns

string: The trailing name component of the given path.

See also

public static function byteLength(string $string): int

Returns the number of bytes in the given string.

This method ensures the string is treated as a byte array by using mb_strlen().

Parameters

ParameterTypeDescription
$stringstringThe string being measured for length

Returns

int: The number of bytes in the given string.

public static function byteSubstr(string $string, int $start, int|null $length = null): string

Returns 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

ParameterTypeDescription
$stringstringThe input string. Must be one character or longer.
$startintThe starting position
$lengthint|nullThe 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

public static function convertIniSizeToBytes(string $string): int

Converts php.ini style size to bytes.

Parameters

ParameterTypeDescription
$stringstringPhp.ini style size. Examples: 512M, 1024K, 1G, 256.

Returns

int: The number of bytes equivalent to the specified string.

Since

2.0.54

public static function countWords(string $string): int

Counts words in a string.

Parameters

ParameterTypeDescription
$stringstringThe text to calculate

Returns

int

Since

2.0.8

public static function dirname(string $path): string

Returns 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

ParameterTypeDescription
$pathstringA path string.

Returns

string: The parent directory’s path.

See also

public static function endsWith(string $string, string $with, bool $caseSensitive = true): bool

Check if given string ends with specified substring. Binary and multibyte safe.

Parameters

ParameterTypeDescription
$stringstringInput string to check
$withstringPart to search inside of the $string.
$caseSensitiveboolCase 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

public static function explode(string $string, string $delimiter = ',', mixed $trim = true, bool $skipEmpty = false): array

Explodes string into array, optionally trims values and skips empty ones.

Parameters

ParameterTypeDescription
$stringstringString to be exploded.
$delimiterstringDelimiter. Default is ’,’.
$trimmixedWhether 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.
$skipEmptyboolWhether to skip empty strings between delimiters. Default is false.

Returns

array

Since

2.0.4

public static function findBetween(string $string, string $start, string $end): 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.

Parameters

ParameterTypeDescription
$stringstringThe input string.
$startstringThe string marking the start of the portion to extract.
$endstringThe 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.

public static function floatToString(float|int $number): string

Safely casts a float to string independent of the current locale.

The decimal separator will always be ..

Parameters

ParameterTypeDescription
$numberfloat|intA floating point number or integer.

Returns

string: The string representation of the number.

Since

2.0.13

public static function mask(string $string, int $start, int $length, string $mask = '*'): string

Masks a portion of a string with a repeated character.

This method is multibyte-safe.

Parameters

ParameterTypeDescription
$stringstringThe input string.
$startintThe 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.
$lengthintThe length of the section to be masked. The masking will start from the $start position and continue for $length characters.
$maskstringThe character to use for masking. The default is ’*’.

Returns

string: The masked string.

public static function matchWildcard(string $pattern, string $string, array $options = []): bool

Checks 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

ParameterTypeDescription
$patternstringThe shell wildcard pattern.
$stringstringThe tested string.
$optionsarrayOptions 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

public static function mb_ucfirst(string $string, string $encoding = 'UTF-8'): string

This method provides a unicode-safe implementation of built-in PHP function ucfirst().

Parameters

ParameterTypeDescription
$stringstringThe string to be proceeded
$encodingstringOptional, defaults to “UTF-8”

Returns

string

Since

2.0.16

See also

public static function mb_ucwords(string $string, string $encoding = 'UTF-8'): string

This method provides a unicode-safe implementation of built-in PHP function ucwords().

Parameters

ParameterTypeDescription
$stringstringThe string to be proceeded
$encodingstringOptional, defaults to “UTF-8”

Returns

string

Since

2.0.16

See also

public static function normalizeNumber(int|float|string $value): string

Returns string representation of number value with replaced commas to dots, if decimal point of current locale is comma.

Parameters

ParameterTypeDescription
$valueint|float|stringThe value to normalize.

Returns

string

Since

2.0.11

public static function startsWith(string $string, string $with, bool $caseSensitive = true): bool

Check if given string starts with specified substring. Binary and multibyte safe.

Parameters

ParameterTypeDescription
$stringstringInput string
$withstringPart to search inside the $string
$caseSensitiveboolCase 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

public static function truncate(string $string, int $length, string $suffix = '...', string|null $encoding = null, bool $asHtml = false): string

Truncates 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

ParameterTypeDescription
$stringstringThe string to truncate.
$lengthintHow many characters from original string to include into truncated string.
$suffixstringString to append to the end of truncated string.
$encodingstring|nullThe charset to use, defaults to charset currently used by application.
$asHtmlboolWhether 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.

public static function truncateWords(string $string, int $count, string $suffix = '...', bool $asHtml = false): string

Truncates a string to the number of words specified.

Parameters

ParameterTypeDescription
$stringstringThe string to truncate.
$countintHow many words from original string to include into truncated string.
$suffixstringString to append to the end of truncated string.
$asHtmlboolWhether 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.

protected static function truncateHtml(string $string, int $count, string $suffix, string|bool $encoding = false): string

Truncate a string while preserving the HTML.

Parameters

ParameterTypeDescription
$stringstringThe string to truncate
$countintThe counter
$suffixstringString to append to the end of the truncated string.
$encodingstring|boolEncoding flag or charset.

Returns

string

Since

2.0.1