Oihana PHP

pad.php

Table of Contents

Functions

pad()  : string
Pads a UTF-8 string to a certain length using a specified padding string and type.

Functions

pad()

Pads a UTF-8 string to a certain length using a specified padding string and type.

pad(string|null $source, int $size, string $pad, int $type) : string

If the source string is null, it is treated as an empty string. The padding uses grapheme clusters to correctly handle multibyte characters.

Parameters
$source : string|null

The input string or null.

$size : int

Desired length after padding (in grapheme clusters).

$pad : string

The string to use for padding (cannot be empty).

$type : int

One of STR_PAD_LEFT, STR_PAD_RIGHT, STR_PAD_BOTH.

Tags
throws
InvalidArgumentException

If the padding type is invalid or $pad is empty.

example
use function oihana\core\strings\pad;

echo pad('hello', 10, ' ', STR_PAD_RIGHT); // Outputs: "hello     "
echo pad('hello', 10, '☺', STR_PAD_LEFT);  // Outputs: "☺☺☺☺☺hello"
echo pad('hello', 10, 'ab', STR_PAD_BOTH); // Outputs: "ababahelloaba"
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
string

The padded string.


        
On this page

Search results