Content · Programming · PHP

PHP mb_str_pad() Function: Usage and Examples

Learn how to pad a Unicode string to a target character length in PHP with syntax, clear code examples, practical use, and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

When working with PHP, you may need to pad a Unicode string to a target character length. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.

mb_str_pad: Unlike str_pad(), this PHP 8.3+ function counts multibyte characters rather than raw bytes.

What Is mb_str_pad in PHP?

Pad a Unicode string to a target character length. Unlike str_pad(), this PHP 8.3+ function counts multibyte characters rather than raw bytes.

Syntax:

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

Parameters and return value: The signature above identifies the arguments and return type where the API has a single call. For topic guides, follow the sequence of calls in the examples. Always distinguish a returned value from an object or variable modified by reference.

Important: Requires PHP 8.3+. Character count and terminal display width may still differ for some Unicode text.

Example 1: Basic Usage

This example introduces the operation with a short and predictable input.

<?php

echo "[" . mb_str_pad("Café", 8, ".", STR_PAD_RIGHT, "UTF-8") . "]";

The code focuses on the main operation. Unlike str_pad(), this PHP 8.3+ function counts multibyte characters rather than raw bytes.

Example 2: Another Case

Now compare a different input, locale, date, or option to see another part of the behavior.

<?php

echo "[" . mb_str_pad("猫", 5, "-", STR_PAD_LEFT, "UTF-8") . "]";

What changes? Requires PHP 8.3+. Character count and terminal display width may still differ for some Unicode text.

Practical Example: Applying It in an Application

Use the following separate example as a starting point for a small application task. Check any extension or environment requirements below before running it.

<?php

$names = ["Ana", "Björn", "Zoë"];
foreach ($names as $name) {
    echo mb_str_pad($name, 12, " ", STR_PAD_RIGHT, "UTF-8") . "| active" . PHP_EOL;
}

The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. Unlike str_pad(), this PHP 8.3+ function counts multibyte characters rather than raw bytes.

Important Behavior and Edge Cases

  • Core behavior: Unlike str_pad(), this PHP 8.3+ function counts multibyte characters rather than raw bytes.
  • Inputs and results: Requires PHP 8.3+. Character count and terminal display width may still differ for some Unicode text.
  • Alternatives: mb_strwidth() measures display width; str_pad() measures bytes.
  • Compatibility: mbstring is required for mb_* functions.
  • Compatibility: This function was introduced in PHP 8.3.

Comparison With Related Functions

Function or ApproachWhen to Use It
mb_str_padPad a unicode string to a target character length.
Related PHP approachmb_strwidth() measures display width; str_pad() measures bytes.

Common Beginner Mistakes

MistakeCorrect Approach
Using an incorrect input or optionRequires PHP 8.3+. Character count and terminal display width may still differ for some Unicode text.
Confusing this API with a related operationmb_strwidth() measures display width; str_pad() measures bytes.
Assuming output is identical across environmentsCheck return values and required extensions or versions. mbstring is required for mb_* functions. This function was introduced in PHP 8.3.

Key Takeaways

  • mb_str_pad helps you pad a Unicode string to a target character length.
  • Unlike str_pad(), this PHP 8.3+ function counts multibyte characters rather than raw bytes.
  • Requires PHP 8.3+. Character count and terminal display width may still differ for some Unicode text.

Use mb_str_pad when you need to pad a Unicode string to a target character length. Start with the first example, compare the second, and adapt the practical example to your inputs. Confirm the return value and environment requirements before adding it to production code.

Share this resourceSend this page to someone who may find it useful.