PHP Tricks
September 1

How to use str_pad function with Unicode string?

Data: Array of Unicode strings (football in different languages):

  [
    'football',
    'футбол', // football in russian
    'ფეხბურთი', // football in georgian
    'хөл бөмбөг', // football in mongolian
    'bóng đá'  // football in vietnamese
  ]

The problem: Can not pad Unicode strings to same length using builtin function

  
<?php
// assign data to variables
$games = [
    'football',
    'футбол', // football in russian
    'ფეხბურთი', // football in georgian
    'хөл бөмбөг', // football in mongolian
    'bóng đá'  // football in vietnamese
];

// loop through $games array and print rows with built-in str_pad function
foreach ($games as $game) {
    echo str_pad($game, 20) . '|' . PHP_EOL;
}

Wrong result: strings have different lengths after apply str_pad() function

  football            |
  футбол        |
  ფეხბურთი|
  хөл бөмбөг |
  bóng đá          |
  
<?php
function mb_str_pad( $input, $pad_length, $pad_string = ' ', $pad_type = STR_PAD_RIGHT) {
    $diff = strlen( $input ) - mb_strlen( $input );
    return str_pad( $input, $pad_length + $diff, $pad_string, $pad_type );
}

// loop through $games array and print rows with custom mb_str_pad function
foreach ($games as $game) {
    echo mb_str_pad($game, 20) . '|' . PHP_EOL;
}

Result: the strings have same lengths after apply custom function mb_str_pad()

  football            |
  футбол              |
  ფეხბურთი           |
  хөл бөмбөг          |
  bóng đá             |

PHP code online