# BaseStringHelper

> BaseStringHelper provides concrete implementation for yii\helpers\StringHelper.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [helpers](/docs/api/yii/helpers/index.md) › BaseStringHelper

| class | `yii\helpers\BaseStringHelper` |
| --- | --- |
| Since | 2.0 |
| Source | [`framework/helpers/BaseStringHelper.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/helpers/BaseStringHelper.php#L22) |

1 subclass:

- [`StringHelper`](/docs/api/yii/helpers/stringhelper/index.md)

BaseStringHelper provides concrete implementation for [`StringHelper`](/docs/api/yii/helpers/stringhelper/index.md).

Do not use BaseStringHelper. Use [`StringHelper`](/docs/api/yii/helpers/stringhelper/index.md) instead.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`base64UrlDecode()`](#method-base64UrlDecode) static | Decodes "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648). | `BaseStringHelper` |
| [`base64UrlEncode()`](#method-base64UrlEncode) static | Encodes string into "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648). | `BaseStringHelper` |
| [`basename()`](#method-basename) static | Returns the trailing name component of a path. | `BaseStringHelper` |
| [`byteLength()`](#method-byteLength) static | Returns the number of bytes in the given string. | `BaseStringHelper` |
| [`byteSubstr()`](#method-byteSubstr) static | Returns the portion of string specified by the start and length parameters. | `BaseStringHelper` |
| [`convertIniSizeToBytes()`](#method-convertIniSizeToBytes) static | Converts php.ini style size to bytes. | `BaseStringHelper` |
| [`countWords()`](#method-countWords) static | Counts words in a string. | `BaseStringHelper` |
| [`dirname()`](#method-dirname) static | Returns parent directory's path. | `BaseStringHelper` |
| [`endsWith()`](#method-endsWith) static | Check if given string ends with specified substring. Binary and multibyte safe. | `BaseStringHelper` |
| [`explode()`](#method-explode) static | Explodes string into array, optionally trims values and skips empty ones. | `BaseStringHelper` |
| [`findBetween()`](#method-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()`](#method-floatToString) static | Safely casts a float to string independent of the current locale. | `BaseStringHelper` |
| [`mask()`](#method-mask) static | Masks a portion of a string with a repeated character. | `BaseStringHelper` |
| [`matchWildcard()`](#method-matchWildcard) static | Checks if the passed string would match the given shell wildcard pattern. | `BaseStringHelper` |
| [`mb_ucfirst()`](#method-mb_ucfirst) static | This method provides a unicode-safe implementation of built-in PHP function `ucfirst()`. | `BaseStringHelper` |
| [`mb_ucwords()`](#method-mb_ucwords) static | This method provides a unicode-safe implementation of built-in PHP function `ucwords()`. | `BaseStringHelper` |
| [`normalizeNumber()`](#method-normalizeNumber) static | Returns string representation of number value with replaced commas to dots, if decimal point of current locale is comma. | `BaseStringHelper` |
| [`startsWith()`](#method-startsWith) static | Check if given string starts with specified substring. Binary and multibyte safe. | `BaseStringHelper` |
| [`truncate()`](#method-truncate) static | Truncates a string to the number of characters specified. | `BaseStringHelper` |
| [`truncateWords()`](#method-truncateWords) static | Truncates a string to the number of words specified. | `BaseStringHelper` |
| [`truncateHtml()`](#method-truncateHtml) protected static | Truncate a string while preserving the HTML. | `BaseStringHelper` |

## Method details

### base64UrlDecode() <span id="method-base64UrlDecode"></span>

```php
public static function base64UrlDecode(string $input): string
```

Decodes "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**

- <https://tools.ietf.org/html/rfc4648#page-7>

### base64UrlEncode() <span id="method-base64UrlEncode"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$input` | `string` | The string to encode. |

**Returns**

`string`: Encoded string.

**Since**

2.0.12

**See also**

- <https://tools.ietf.org/html/rfc4648#page-7>

### basename() <span id="method-basename"></span>

```php
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**

| 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**

- <https://www.php.net/manual/en/function.basename.php>

### byteLength() <span id="method-byteLength"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$string` | `string` | The string being measured for length |

**Returns**

`int`: The number of bytes in the given string.

### byteSubstr() <span id="method-byteSubstr"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$string` | `string` | The input string. Must be one character or longer. |
| `$start` | `int` | The starting position |
| `$length` | `int\|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. |

**Returns**

`string`: The extracted part of string, or FALSE on failure or an empty string.

**See also**

- <https://www.php.net/manual/en/function.substr.php>

### convertIniSizeToBytes() <span id="method-convertIniSizeToBytes"></span>

```php
public static function convertIniSizeToBytes(string $string): int
```

Converts 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() <span id="method-countWords"></span>

```php
public static function countWords(string $string): int
```

Counts words in a string.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$string` | `string` | The text to calculate |

**Returns**

`int`

**Since**

2.0.8

### dirname() <span id="method-dirname"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$path` | `string` | A path string. |

**Returns**

`string`: The parent directory's path.

**See also**

- <https://www.php.net/manual/en/function.basename.php>

### endsWith() <span id="method-endsWith"></span>

```php
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**

| 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() <span id="method-explode"></span>

```php
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**

| 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() <span id="method-findBetween"></span>

```php
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**

| 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() <span id="method-floatToString"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$number` | `float\|int` | A floating point number or integer. |

**Returns**

`string`: The string representation of the number.

**Since**

2.0.13

### mask() <span id="method-mask"></span>

```php
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**

| 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() <span id="method-matchWildcard"></span>

```php
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()`](https://www.php.net/manual/en/function.fnmatch.php), 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() <span id="method-mb_ucfirst"></span>

```php
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**

| 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**

- <https://www.php.net/manual/en/function.ucfirst.php>

### mb_ucwords() <span id="method-mb_ucwords"></span>

```php
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**

| 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**

- <https://www.php.net/manual/en/function.ucwords>

### normalizeNumber() <span id="method-normalizeNumber"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `int\|float\|string` | The value to normalize. |

**Returns**

`string`

**Since**

2.0.11

### startsWith() <span id="method-startsWith"></span>

```php
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**

| 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() <span id="method-truncate"></span>

```php
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**

| 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\|null` | 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() <span id="method-truncateWords"></span>

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

Truncates 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() <span id="method-truncateHtml"></span>

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

Truncate 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\|bool` | Encoding flag or charset. |

**Returns**

`string`

**Since**

2.0.1