CKTIS / PHP
STR_PAD_LEFT
STR_PAD_LEFT selects left padding in str_pad to reach a target byte length.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
str_pad($text, $length, $pad, STR_PAD_LEFT)STR_PAD_LEFT selects left padding in str_pad to reach a target byte length. The padding pattern repeats and is truncated to exactly the missing bytes. An input already long enough is returned unchanged. Multibyte text can therefore reach the byte target with fewer visible characters, and truncating a multibyte pad may produce invalid UTF-8.
echo json_encode([str_pad("7", 4, "ab", STR_PAD_LEFT), str_pad("long", 2, "0", STR_PAD_LEFT)]), PHP_EOL;
$padded = str_pad("é", 4, "0", STR_PAD_LEFT);
echo strlen($padded), " ", bin2hex(str_pad("A", 2, "é", STR_PAD_LEFT));
["aba7","long"]
4 c341
Examples
<?php
echo json_encode([str_pad("7", 4, "ab", STR_PAD_LEFT), str_pad("long", 2, "0", STR_PAD_LEFT)]), PHP_EOL;
$padded = str_pad("é", 4, "0", STR_PAD_LEFT);
echo strlen($padded), " ", bin2hex(str_pad("A", 2, "é", STR_PAD_LEFT));
Result
["aba7","long"] 4 c341