@@ -93,6 +93,10 @@ public static function convertCharset($input, $from, $to, $force = false)
93
93
return $ input ;
94
94
}
95
95
96
+ if (strlen ($ input ) === 0 ) {
97
+ return $ input ;
98
+ }
99
+
96
100
return self ::_convertCharset ($ input , $ from , $ to );
97
101
}
98
102
@@ -145,12 +149,7 @@ protected static function _convertCharset($input, $from, $to)
145
149
146
150
/* Try iconv with transliteration. */
147
151
if (Horde_Util::extensionExists ('iconv ' )) {
148
- unset($ php_errormsg );
149
- ini_set ('track_errors ' , 1 );
150
- $ out = @iconv ($ from , $ to . '//TRANSLIT ' , $ input );
151
- $ errmsg = isset ($ php_errormsg );
152
- ini_restore ('track_errors ' );
153
- if (!$ errmsg && $ out !== false ) {
152
+ if (($ out = self ::_convertCharsetIconv ($ input , $ from , $ to )) !== false ) {
154
153
return $ out ;
155
154
}
156
155
}
@@ -170,6 +169,25 @@ protected static function _convertCharset($input, $from, $to)
170
169
return $ input ;
171
170
}
172
171
172
+ /**
173
+ * Internal function used to do charset transliteration with iconv.
174
+ *
175
+ * @param string $input See self::convertCharset().
176
+ * @param string $from See self::convertCharset().
177
+ * @param string $to See self::convertCharset().
178
+ *
179
+ * @return mixed The converted string, or false on error.
180
+ */
181
+ protected static function _convertCharsetIconv (string $ input , string $ from , string $ to ): string
182
+ {
183
+ error_clear_last ();
184
+ $ out = @iconv ($ from , $ to . '//TRANSLIT ' , $ input );
185
+ if (is_null (error_get_last ()) && $ out !== false ) {
186
+ return $ out ;
187
+ }
188
+ return false ;
189
+ }
190
+
173
191
/**
174
192
* Makes a string lowercase.
175
193
*
@@ -488,37 +506,77 @@ protected static function _pos(
488
506
)
489
507
{
490
508
if (Horde_Util::extensionExists ('mbstring ' )) {
491
- unset($ php_errormsg );
492
- $ track_errors = ini_set ('track_errors ' , 1 );
493
- $ ret = @call_user_func ('mb_ ' . $ func , $ haystack , $ needle , $ offset , self ::_mbstringCharset ($ charset ));
494
- ini_set ('track_errors ' , $ track_errors );
495
- if (!isset ($ php_errormsg )) {
496
- return $ ret ;
509
+ if (($ out = @self ::_posMbstring ($ haystack , $ needle , $ offset , $ charset , $ func )) !== false ) {
510
+ return $ out ;
497
511
}
498
512
}
499
513
500
514
if (Horde_Util::extensionExists ('intl ' )) {
501
- unset($ php_errormsg );
502
- $ track_errors = ini_set ('track_errors ' , 1 );
503
- $ ret = self ::convertCharset (
504
- @call_user_func (
505
- 'grapheme_ ' . $ func ,
506
- self ::convertCharset ($ haystack , $ charset , 'UTF-8 ' ),
507
- self ::convertCharset ($ needle , $ charset , 'UTF-8 ' ),
508
- $ offset
509
- ),
510
- 'UTF-8 ' ,
511
- $ charset
512
- );
513
- ini_set ('track_errors ' , $ track_errors );
514
- if (!isset ($ php_errormsg )) {
515
- return $ ret ;
515
+ if (($ out = @self ::_posIntl ($ haystack , $ needle , $ offset , $ charset , $ func )) !== false ) {
516
+ return $ out ;
516
517
}
517
518
}
518
519
519
520
return $ func ($ haystack , $ needle , $ offset );
520
521
}
521
522
523
+ /**
524
+ * Internal function to perform string position searches using mbstring.
525
+ *
526
+ * @param string $haystack See self::_pos
527
+ * @param string $needle See self::_pos
528
+ * @param integer $offset See self::_pos
529
+ * @param string $charset See self::_pos
530
+ * @param string $func See self::_pos
531
+ *
532
+ * @return mixed The position of occurrence, or false on error.
533
+ */
534
+ protected static function _posMbstring (
535
+ $ haystack , $ needle , $ offset , $ charset , $ func
536
+ )
537
+ {
538
+ error_clear_last ();
539
+ $ ret = @call_user_func ('mb_ ' . $ func , $ haystack , $ needle , $ offset , self ::_mbstringCharset ($ charset ));
540
+ if (is_null (error_get_last ())) {
541
+ return $ ret ;
542
+ }
543
+
544
+ return false ;
545
+ }
546
+
547
+ /**
548
+ * Internal function to perform string position searches using intl.
549
+ *
550
+ * @param string $haystack See self::_pos
551
+ * @param string $needle See self::_pos
552
+ * @param integer $offset See self::_pos
553
+ * @param string $charset See self::_pos
554
+ * @param string $func See self::_pos
555
+ *
556
+ * @return mixed The position of occurrence, or false on error.
557
+ */
558
+ protected static function _posIntl (
559
+ $ haystack , $ needle , $ offset , $ charset , $ func
560
+ )
561
+ {
562
+ error_clear_last ();
563
+ $ ret = self ::convertCharset (
564
+ @call_user_func (
565
+ 'grapheme_ ' . $ func ,
566
+ self ::convertCharset ($ haystack , $ charset , 'UTF-8 ' ),
567
+ self ::convertCharset ($ needle , $ charset , 'UTF-8 ' ),
568
+ $ offset
569
+ ),
570
+ 'UTF-8 ' ,
571
+ $ charset
572
+ );
573
+ if (is_null (error_get_last ())) {
574
+ return $ ret ;
575
+ }
576
+
577
+ return false ;
578
+ }
579
+
522
580
/**
523
581
* Returns a string padded to a certain length with another string.
524
582
* This method behaves exactly like str_pad() but is multibyte safe.
0 commit comments