Skip to content

Commit a268b6d

Browse files
using mb_strtoupper and mb_strtolower normalize lower() and upper() method to handle advance use cases
1 parent 2d82f49 commit a268b6d

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

Str.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ public static function replace($search, $replace, $subject = null)
902902
*/
903903
public static function lower($value = null)
904904
{
905-
return strtolower(self::trim($value));
905+
return self::normalize($value);
906906
}
907907

908908
/**
@@ -913,7 +913,7 @@ public static function lower($value = null)
913913
*/
914914
public static function upper($value = null)
915915
{
916-
return strtoupper(self::trim($value));
916+
return self::normalize($value, 'upper');
917917
}
918918

919919
/**

Traits/StrTrait.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,30 @@ public static function padString(string $value, int $length, string $padChar = '
167167
return str_pad($value, $length, $padChar, $padType);
168168
}
169169

170+
/**
171+
* Normalize a string: trim and convert case.
172+
*
173+
* @param string|null $value
174+
* @param string $case 'lower' or 'upper'
175+
* @param string $encoding Optional, for multibyte support
176+
* @return string
177+
*/
178+
private static function normalize($value = null, string $case = 'lower', string $encoding = 'UTF-8'): string
179+
{
180+
$value = self::trim($value);
181+
182+
if ($case === 'upper') {
183+
return function_exists('mb_strtoupper')
184+
? mb_strtoupper(self::trim($value), $encoding)
185+
: strtoupper(self::trim($value));
186+
}
187+
188+
// default: lower
189+
return function_exists('mb_strtolower')
190+
? mb_strtolower(self::trim($value), $encoding)
191+
: strtolower(self::trim($value));
192+
}
193+
170194
/**
171195
* Replace Subject
172196
*

0 commit comments

Comments
 (0)