Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

mb_trim

Strips whitespace from both ends of a string, including multibyte whitespace.

Syntax

mb_trim(string $string, ?string $characters = null, ?string $encoding = null): string

Parameters

ParameterTypeDescription
$stringstringThe input string.
$characters?stringOptional. Which characters to strip.
$encoding?stringOptional. 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 4

Notes

The function appeared in PHP 8.4 together with mb_ltrim() and mb_rtrim().

See also

  • trim — Removes whitespace and other characters from both ends of a string.
  • mb_strlen — Returns the number of characters in a string.
  • mb_substr — Returns part of a string.

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.

Thanks for reading!

Donate to the site — we work hard for you.