Created
March 19, 2020 12:10
-
-
Save DAVIDhaker/04efb6abdcba85b6be234dd147d718ba to your computer and use it in GitHub Desktop.
Pad unicode string right (like Python str.ljust)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function ljust($str, $len, $fill_char) { | |
$str_len = mb_strlen($str); | |
$out = range(0, max($str_len, $len)); | |
$out_len = count($out); | |
for ($i = 0; $i < $out_len; $i++) | |
$out[$i] = $i < $str_len ? mb_substr($str, $i, 1) : $fill_char; | |
return implode("", $out); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment