Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

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): string

Parameters

ParameterTypeDescription
$stringstringThe input string.
$lengthintThe target length in characters.
$pad_stringstringOptional. What to pad with.
$pad_typeintOptional. The side: `STR_PAD_RIGHT`, `STR_PAD_LEFT`, `STR_PAD_BOTH`.
$encoding?stringOptional. 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.

Thanks for reading!

Donate to the site — we work hard for you.