Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

str_pad()

Pad to a minimum byte length on the left, right or both sides.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

str_pad(string $string, int $length, string $pad_string = " ", int $pad_type = STR_PAD_RIGHT): string

Pad to a minimum byte length on the left, right or both sides. A multibyte pad string may be cut mid-character when only some bytes are needed. In two-sided padding an odd extra byte goes on the right. An empty pad string raises ValueError only when padding is necessary. Otherwise the original string is returned.

Signature

str_pad(string $string, int $length, string $pad_string = " ", int $pad_type = STR_PAD_RIGHT): string

Example output

[".X..","long","c3"]

Examples

<?php
echo json_encode([str_pad("X",4,".",STR_PAD_BOTH),str_pad("long",2,""),bin2hex(str_pad("",1,"\u{00E9}"))]);

Result

[".X..","long","c3"]

Sources