mb_trim
Strips whitespace from both ends of a string, including multibyte whitespace.
Syntax
mb_trim(string $string, ?string $characters = null, ?string $encoding = null): stringParameters
| Parameter | Type | Description |
|---|---|---|
| $string | string | The input string. |
| $characters | ?string | Optional. Which characters to strip. |
| $encoding | ?string | Optional. The encoding. |
Return value
stringThe string without surrounding whitespace.
Examples
A non-breaking space that trim() leaves in place:
<?php
$text = "\u{00A0}Anna\u{00A0}";
echo strlen(trim($text)), ' ', strlen(mb_trim($text));Output:
8 4Notes
The function appeared in PHP 8.4 together with mb_ltrim() and mb_rtrim().
See also
In the tutorial
- New in PHP 8.4 — PHP 8.4 features — property hooks, asymmetric visibility, calling a method right after new, and new array functions.