Content · Programming · PHP

PHP mb_trim() Function: Usage and Examples

Learn how to remove leading and trailing Unicode whitespace 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 remove leading and trailing Unicode whitespace. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.

mb_trim: PHP 8.4+ supports Unicode-aware trimming from both string ends.

What Is mb_trim in PHP?

Remove leading and trailing Unicode whitespace. PHP 8.4+ supports Unicode-aware trimming from both string ends.

Syntax:

mb_trim(string $string, ?string $characters = null, ?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.4+. Trimming removes only edge characters, not whitespace inside the text.

Example 1: Basic Usage

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

<?php

echo "[" . mb_trim("  Café  ", null, "UTF-8") . "]";

The code focuses on the main operation. PHP 8.4+ supports Unicode-aware trimming from both string ends.

Example 2: Another Case

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

<?php

echo mb_trim("***hello***", "*", "UTF-8");

What changes? Requires PHP 8.4+. Trimming removes only edge characters, not whitespace inside the 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

$usernames = [" Alice ", " Bob  ", " Chloé "];
foreach ($usernames as $username) {
    echo "[" . mb_trim($username, null, "UTF-8") . "]" . PHP_EOL;
}

The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. PHP 8.4+ supports Unicode-aware trimming from both string ends.

Important Behavior and Edge Cases

  • Core behavior: PHP 8.4+ supports Unicode-aware trimming from both string ends.
  • Inputs and results: Requires PHP 8.4+. Trimming removes only edge characters, not whitespace inside the text.
  • Alternatives: mb_ltrim() and mb_rtrim() target only one side.
  • Compatibility: mbstring is required for mb_* functions.
  • Compatibility: This function was introduced in PHP 8.4.

Comparison With Related Functions

Function or ApproachWhen to Use It
mb_trimRemove leading and trailing unicode whitespace.
Related PHP approachmb_ltrim() and mb_rtrim() target only one side.

Common Beginner Mistakes

MistakeCorrect Approach
Using an incorrect input or optionRequires PHP 8.4+. Trimming removes only edge characters, not whitespace inside the text.
Confusing this API with a related operationmb_ltrim() and mb_rtrim() target only one side.
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.4.

Key Takeaways

  • mb_trim helps you remove leading and trailing Unicode whitespace.
  • PHP 8.4+ supports Unicode-aware trimming from both string ends.
  • Requires PHP 8.4+. Trimming removes only edge characters, not whitespace inside the text.

Use mb_trim when you need to remove leading and trailing Unicode whitespace. 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.