PHP 8 reference · Multibyte String Functions
PHP mb_trim() Function
Strip whitespace from both ends of a string, including Unicode spaces such as the non-breaking space. Added in PHP 8.4.
PHP 8 Since 8.4
Syntax
mb_trim(string $string, ?string $characters = null, ?string $encoding = null): string
Parameters
| Parameter | Description |
|---|---|
string |
The input string. |
characters optional |
The characters to strip; by default every Unicode whitespace character. |
encoding optional |
The character encoding; defaults to mb_internal_encoding() (UTF-8). |
Return value
The trimmed string.
Examples
<?php
$input = "\u{00A0} hello\u{3000}";
var_dump(trim($input));
var_dump(mb_trim($input));
Output
string(12) " hello " string(5) "hello"
Run this on your computer — see “Get Started” for setup.
<?php
echo mb_trim("«Bonjour»", "«»"), "\n";
echo mb_ltrim("¡¡Hola"), "|", mb_ltrim("¡¡Hola", "¡"), "\n";
Output
Bonjour ¡¡Hola|Hola
Run this on your computer — see “Get Started” for setup.
mb_ltrim() and mb_rtrim(), also added in PHP 8.4, strip only the start or the end.
Version history
| Version | Change |
|---|---|
| PHP 8.4 | Added |
In other languages
str.strip()· Pythonstring.trim()· JavaScript
Related
trim()· Remove whitespace (or other characters you choose) from both ends of a string. Spaces inside the string are kept.ltrim()· Remove whitespace or other chosen characters from the start (left side) of a string only.rtrim()· Remove whitespace or other chosen characters from the end (right side) of a string. chop() is an alias.- PHP Strings · tutorial