3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function mb_str_pad($str, $pad_len, $pad_str = ' ', $dir = STR_PAD_RIGHT, $encoding = NULL) { $encoding = $encoding === NULL ? mb_internal_encoding() : $encoding; $padBefore = $dir === STR_PAD_BOTH || $dir === STR_PAD_LEFT; $padAfter = $dir === STR_PAD_BOTH || $dir === STR_PAD_RIGHT; $pad_len -= mb_strlen($str, $encoding); $targetLen = $padBefore && $padAfter ? $pad_len / 2 : $pad_len; $strToRepeatLen = mb_strlen($pad_str, $encoding); $repeatTimes = ceil($targetLen / $strToRepeatLen); $repeatedString = str_repeat($pad_str, max(0, $repeatTimes)); // safe if used with valid utf-8 strings $before = $padBefore ? mb_substr($repeatedString, 0, floor($targetLen), $encoding) : ''; $after = $padAfter ? mb_substr($repeatedString, 0, ceil($targetLen), $encoding) : ''; return $before . $str . $after; } // same random testing... foreach([STR_PAD_BOTH, STR_PAD_LEFT, STR_PAD_RIGHT] as $padType) { echo str_pad("FOO", 11, "✓", $padType) . "\n"; echo mb_str_pad("FOO", 11, "àèò", $padType, "UTF-8") . "\n"; echo str_pad("FOO", 10, "aeo", $padType) . "\n"; echo mb_str_pad("FOO", 10, "àèò", $padType, "UTF-8") . "\n"; echo str_pad("BAAZ", 11, "aeo", $padType) . "\n"; echo mb_str_pad("BAAZ", 11, "àèò", $padType, "UTF-8") . "\n"; echo str_pad("BAAZ", 10, "aeo", $padType) . "\n"; echo mb_str_pad("BAAZ", 10, "àèò", $padType, "UTF-8") . "\n"; echo str_pad("FOOBAR", 6, "aeo", $padType) . "\n"; echo mb_str_pad("FOOBAR", 6, "àèò", $padType, "UTF-8") . "\n"; echo str_pad("FOOBAR", 1, "aeo", $padType) . "\n"; echo mb_str_pad("FOOBAR", 1, "àèò", $padType, "UTF-8") . "\n"; echo str_pad("FOOBAR", 0, "aeo", $padType) . "\n"; echo mb_str_pad("FOOBAR", 0, "àèò", $padType, "UTF-8") . "\n"; echo str_pad("FOOBAR", -10, "aeo", $padType) . "\n"; echo mb_str_pad("FOOBAR", -10, "àèò", $padType, "UTF-8") . "\n"; echo "--\n"; } ?>
based on UnXTF
Output for 5.4.0 - 7.2.0
✓�FOO✓� àèòàFOOàèòà aeoFOOaeoa àèòFOOàèòà aeoBAAZaeoa àèòBAAZàèòà aeoBAAZaeo àèòBAAZàèò FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR -- ✓✓�FOO àèòàèòàèFOO aeoaeoaFOO àèòàèòàFOO aeoaeoaBAAZ àèòàèòàBAAZ aeoaeoBAAZ àèòàèòBAAZ FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR -- FOO✓✓� FOOàèòàèòàè FOOaeoaeoa FOOàèòàèòà BAAZaeoaeoa BAAZàèòàèòà BAAZaeoaeo BAAZàèòàèò FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR --
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/pY3MR on line 20
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/pY3MR on line 20
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/pY3MR on line 20
Process exited with code 255.