@@ -60,8 +60,27 @@ public function offsetUnset($offset)
6060 */
6161 protected function formatString ($ str )
6262 {
63- if (function_exists ('mb_convert_case ' )) {
63+ if (extension_loaded ('mbstring ' )) {
64+ $ originalStr = $ str ;
6465 $ str = mb_convert_case ($ str , MB_CASE_TITLE , 'UTF-8 ' );
66+
67+ // Correct for MB_TITLE_CASE's insistence on uppercasing letters
68+ // immediately preceded by numerals, eg: 1st -> 1St
69+ $ originalEncoding = mb_regex_encoding ();
70+ mb_regex_encoding ('UTF-8 ' );
71+
72+ // matches an upper case letter character immediately preceded by a numeral
73+ mb_ereg_search_init ($ str , '[0-9]\p{Lu} ' );
74+
75+ while ($ match = mb_ereg_search_pos ()) {
76+ $ charPos = $ match [0 ] + 1 ;
77+ // Only swap it back to lowercase if it was lowercase to begin with
78+ if (mb_ereg_match ('\p{Ll} ' , $ originalStr [$ charPos ])) {
79+ $ str [$ charPos ] = mb_strtolower ($ str [$ charPos ]);
80+ }
81+ }
82+
83+ mb_regex_encoding ($ originalEncoding );
6584 } else {
6685 $ str = $ this ->lowerize ($ str );
6786 $ str = ucwords ($ str );
@@ -82,7 +101,7 @@ protected function formatString($str)
82101 */
83102 protected function lowerize ($ str )
84103 {
85- return function_exists ( ' mb_strtolower ' ) ? mb_strtolower ($ str , 'UTF-8 ' ) : strtolower ($ str );
104+ return extension_loaded ( ' mbstring ' ) ? mb_strtolower ($ str , 'UTF-8 ' ) : strtolower ($ str );
86105 }
87106
88107 /**
@@ -94,6 +113,6 @@ protected function lowerize($str)
94113 */
95114 protected function upperize ($ str )
96115 {
97- return function_exists ( ' mb_strtoupper ' ) ? mb_strtoupper ($ str , 'UTF-8 ' ) : strtoupper ($ str );
116+ return extension_loaded ( ' mbstring ' ) ? mb_strtoupper ($ str , 'UTF-8 ' ) : strtoupper ($ str );
98117 }
99118}
0 commit comments