mb_str_pad
Pads a string to a length, counting characters rather than bytes.
Syntax
mb_str_pad(string $string, int $length, string $pad_string = " ", int $pad_type = STR_PAD_RIGHT, ?string $encoding = null): stringParameters
| Parameter | Type | Description |
|---|---|---|
| $string | string | The input string. |
| $length | int | The target length in characters. |
| $pad_string | string | Optional. What to pad with. |
| $pad_type | int | Optional. The side: `STR_PAD_RIGHT`, `STR_PAD_LEFT`, `STR_PAD_BOTH`. |
| $encoding | ?string | Optional. The encoding. |
Return value
stringThe padded string.
Examples
Aligning non-ASCII text:
<?php
echo '[', str_pad('Чай', 6, '.'), "]\n";
echo '[', mb_str_pad('Чай', 6, '.'), ']';Output:
[Чай]
[Чай...]Notes
The function appeared in PHP 8.3. str_pad() counts bytes, so it misaligns multibyte strings.
See also
- str_pad — Pads a string to a given length.
- mb_strlen — Returns the number of characters in a string.
- mb_strimwidth — Truncates a string to a given width and appends a marker.
In the tutorial
- Strings — Working with strings in PHP — quotes, concatenation, length, searching, replacing and multibyte functions.