When working with PHP, you may need to remove leading whitespace or selected Unicode characters. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
mb_ltrim: PHP 8.4+ includes Unicode-aware leading trim, including whitespace characters beyond ASCII.
What Is mb_ltrim in PHP?
Remove leading whitespace or selected Unicode characters. PHP 8.4+ includes Unicode-aware leading trim, including whitespace characters beyond ASCII.
Syntax:
mb_ltrim(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+. A character list describes removable characters, not a whole prefix string.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
echo "[" . mb_ltrim(" Café", null, "UTF-8") . "]";
The code focuses on the main operation. PHP 8.4+ includes Unicode-aware leading trim, including whitespace characters beyond ASCII.
Example 2: Another Case
Now compare a different input, locale, date, or option to see another part of the behavior.
<?php
echo mb_ltrim("***Report", "*", "UTF-8");
What changes? Requires PHP 8.4+. A character list describes removable characters, not a whole prefix string.
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
$submittedNames = [" Ana", " Björn", " Chloé"];
foreach ($submittedNames as $submittedName) {
echo mb_ltrim($submittedName, 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+ includes Unicode-aware leading trim, including whitespace characters beyond ASCII.
Important Behavior and Edge Cases
- Core behavior: PHP 8.4+ includes Unicode-aware leading trim, including whitespace characters beyond ASCII.
- Inputs and results: Requires PHP 8.4+. A character list describes removable characters, not a whole prefix string.
- Alternatives: mb_trim() handles both ends; mb_rtrim() handles only the trailing end.
- Compatibility: mbstring is required for mb_* functions.
- Compatibility: This function was introduced in PHP 8.4.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
mb_ltrim | Remove leading whitespace or selected unicode characters. |
| Related PHP approach | mb_trim() handles both ends; mb_rtrim() handles only the trailing end. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | Requires PHP 8.4+. A character list describes removable characters, not a whole prefix string. |
| Confusing this API with a related operation | mb_trim() handles both ends; mb_rtrim() handles only the trailing end. |
| Assuming output is identical across environments | Check return values and required extensions or versions. mbstring is required for mb_* functions. This function was introduced in PHP 8.4. |
Key Takeaways
- mb_ltrim helps you remove leading whitespace or selected Unicode characters.
- PHP 8.4+ includes Unicode-aware leading trim, including whitespace characters beyond ASCII.
- Requires PHP 8.4+. A character list describes removable characters, not a whole prefix string.
Use mb_ltrim when you need to remove leading whitespace or selected Unicode characters. 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.