PHP 8 reference · Multibyte String Functions

PHP mb_str_pad() Function

Pad a multibyte string to a given length in characters, on the left, right or both sides. Added in PHP 8.3.

PHP 8 Since 8.3

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

mb_str_pad(string $string, int $length, string $pad_string = " ", int $pad_type = STR_PAD_RIGHT, ?string $encoding = null): string

Parameters

ParameterDescription
string The input string.
length The final length in characters.
pad_string optional What to pad with. Default: " "
pad_type optional STR_PAD_RIGHT, STR_PAD_LEFT or STR_PAD_BOTH. Default: STR_PAD_RIGHT
encoding optional The character encoding; defaults to mb_internal_encoding() (UTF-8).

Return value

The padded string.

Examples

Line up names with accentsTry it yourself
<?php
foreach (["Zoë", "Ana", "Jürgen"] as $name) {
    echo "[" . mb_str_pad($name, 8) . "]", "\n";
}

Output

[Zoë     ]
[Ana     ]
[Jürgen  ]
Compared with str_pad()Try it yourself
<?php
echo "[" . str_pad("Zoë", 6) . "]", "\n";    // bytes: one space short
echo "[" . mb_str_pad("Zoë", 6) . "]", "\n"; // characters

Output

[Zoë  ]
[Zoë   ]

Version history

VersionChange
PHP 8.3Added

In other languages

Was this page helpful?