@@ -60,8 +60,27 @@ public function offsetUnset($offset)
60
60
*/
61
61
protected function formatString ($ str )
62
62
{
63
- if (function_exists ('mb_convert_case ' )) {
63
+ if (extension_loaded ('mbstring ' )) {
64
+ $ originalStr = $ str ;
64
65
$ 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 );
65
84
} else {
66
85
$ str = $ this ->lowerize ($ str );
67
86
$ str = ucwords ($ str );
@@ -82,7 +101,7 @@ protected function formatString($str)
82
101
*/
83
102
protected function lowerize ($ str )
84
103
{
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 );
86
105
}
87
106
88
107
/**
@@ -94,6 +113,6 @@ protected function lowerize($str)
94
113
*/
95
114
protected function upperize ($ str )
96
115
{
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 );
98
117
}
99
118
}
0 commit comments