PHP offers trim
, ltrim
and rtrim
functions to remove whitespace (by default) or specified characters from both sides, beginning, or end of the string. These functions are particularly useful for cleaning up user input. However, the trim
, ltrim
, and rtrim
functions do not support multibyte characters, which can lead to issues when working with languages that use multibyte encodings such as UTF-8.
Since PHP 8.4, the mb_trim
, mb_ltrim
and mb_rtrim
functions has become available. These functions support multibyte strings and can trim any multibyte characters as well. The default list of trimmed whitespace characters has been updated to include several other commonly trimmed characters.
<?php
echo '|'.mb_trim(' 卞卞 ').'|'; // |卞卞|
echo '|'.mb_ltrim(' 卞卞 ').'|'; // |卞卞 |
echo '|'.mb_rtrim(' 卞卞 ').'|'; // | 卞卞|
Leave a Comment
Cancel reply