@@ -818,6 +818,50 @@ public static function mb_ord($s, $encoding = null)
818
818
return $ code ;
819
819
}
820
820
821
+ public static function mb_str_pad (string $ string , int $ length , string $ pad_string = ' ' , int $ pad_type = \STR_PAD_RIGHT , string $ encoding = null ): string
822
+ {
823
+ if (!\in_array ($ pad_type , [\STR_PAD_RIGHT , \STR_PAD_LEFT , \STR_PAD_BOTH ], true )) {
824
+ throw new \ValueError ('mb_str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH ' );
825
+ }
826
+
827
+ if (null === $ encoding ) {
828
+ $ encoding = self ::mb_internal_encoding ();
829
+ }
830
+
831
+ try {
832
+ $ validEncoding = @self ::mb_check_encoding ('' , $ encoding );
833
+ } catch (\ValueError $ e ) {
834
+ throw new \ValueError (sprintf ('mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, "%s" given ' , $ encoding ));
835
+ }
836
+
837
+ // BC for PHP 7.3 and lower
838
+ if (!$ validEncoding ) {
839
+ throw new \ValueError (sprintf ('mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, "%s" given ' , $ encoding ));
840
+ }
841
+
842
+ if (self ::mb_strlen ($ pad_string , $ encoding ) <= 0 ) {
843
+ throw new \ValueError ('mb_str_pad(): Argument #3 ($pad_string) must be a non-empty string ' );
844
+ }
845
+
846
+ $ paddingRequired = $ length - self ::mb_strlen ($ string , $ encoding );
847
+
848
+ if ($ paddingRequired < 1 ) {
849
+ return $ string ;
850
+ }
851
+
852
+ switch ($ pad_type ) {
853
+ case \STR_PAD_LEFT :
854
+ return self ::mb_substr (str_repeat ($ pad_string , $ paddingRequired ), 0 , $ paddingRequired , $ encoding ).$ string ;
855
+ case \STR_PAD_RIGHT :
856
+ return $ string .self ::mb_substr (str_repeat ($ pad_string , $ paddingRequired ), 0 , $ paddingRequired , $ encoding );
857
+ default :
858
+ $ leftPaddingLength = floor ($ paddingRequired / 2 );
859
+ $ rightPaddingLength = $ paddingRequired - $ leftPaddingLength ;
860
+
861
+ return self ::mb_substr (str_repeat ($ pad_string , $ leftPaddingLength ), 0 , $ leftPaddingLength , $ encoding ).$ string .self ::mb_substr (str_repeat ($ pad_string , $ rightPaddingLength ), 0 , $ rightPaddingLength , $ encoding );
862
+ }
863
+ }
864
+
821
865
private static function getSubpart ($ pos , $ part , $ haystack , $ encoding )
822
866
{
823
867
if (false === $ pos ) {
0 commit comments