PHPize Online / SQLize Online

Articles    Popular

How to use str_pad function with unicode string?

Tags: PHP 5.x PHP 7.x PHP 8.x

Data: Array of unicode strings (footbal in differnt 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 lenght using buitin function


  

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

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

Solution:


  

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

  football            |
  футбол              |
  ფეხბურთი            |
  хөл бөмбөг          |
  bóng đá             |
  
Use this link for run and share PHP code


If you find an error on this page, please contact us via feedback form