From ef9627532ee13d6815c4689822ea1727c9d6c9a1 Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Fri, 21 Jul 2023 10:34:45 +0200 Subject: [PATCH] Add trim method --- boostrap.php | 13 +++++++++++++ src/MbstringExtended.php | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/boostrap.php b/boostrap.php index f774d7a..c43e212 100644 --- a/boostrap.php +++ b/boostrap.php @@ -2,6 +2,19 @@ use jrosset\MbstringExtended; +if (!function_exists('mb_trim')) { + /** + * Multi-bytes implementation for {@see https://www.php.net/manual/function.trim.php trim} + * + * @param string $string The input string + * @param string $charactersPattern The pattern of characters to trim (delimiter is ”#”, case sensitive) ; default = only spaces + * + * @return string The resulting string + */ + function mb_trim (string $string, string $charactersPattern = '\\s'): string { + return MbstringExtended::trim($string, $charactersPattern); + } +} if (!function_exists('mb_ucfirst')) { /** * Multi-bytes implementation for {@see https://www.php.net/manual/function.ucfirst.php ucfirst} diff --git a/src/MbstringExtended.php b/src/MbstringExtended.php index 9334225..6e36551 100644 --- a/src/MbstringExtended.php +++ b/src/MbstringExtended.php @@ -6,6 +6,18 @@ namespace jrosset; * Class for “missing” mbstring functions */ final class MbstringExtended { + /** + * Multi-bytes implementation for {@see https://www.php.net/manual/function.trim.php trim} + * + * @param string $string The input string + * @param string $charactersPattern The pattern of characters to trim (delimiter is ”#”, case sensitive) ; default = only spaces + * + * @return string The resulting string + */ + public static function trim (string $string, string $charactersPattern = '\\s'): string { + /** @noinspection RegExpUnnecessaryNonCapturingGroup */ + return preg_replace('#(?:^' . $charactersPattern . ')|(?:' . $charactersPattern . '$)#u', '', $string); + } /** * Multi-bytes implementation for {@see https://www.php.net/manual/function.ucfirst.php ucfirst} *