Add trim method

master 1.3.0
Julien Rosset 2 years ago
parent dc4fd13082
commit ef9627532e

@ -2,6 +2,19 @@
use jrosset\MbstringExtended; 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')) { if (!function_exists('mb_ucfirst')) {
/** /**
* Multi-bytes implementation for {@see https://www.php.net/manual/function.ucfirst.php ucfirst} * Multi-bytes implementation for {@see https://www.php.net/manual/function.ucfirst.php ucfirst}

@ -6,6 +6,18 @@ namespace jrosset;
* Class for “missing” mbstring functions * Class for “missing” mbstring functions
*/ */
final class MbstringExtended { 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} * Multi-bytes implementation for {@see https://www.php.net/manual/function.ucfirst.php ucfirst}
* *

Loading…
Cancel
Save