Oihana PHP

padBoth.php

Table of Contents

Functions

padBoth()  : string
Pads a UTF-8 string on both sides (left and right) to a certain length using a specified padding string.

Functions

padBoth()

Pads a UTF-8 string on both sides (left and right) to a certain length using a specified padding string.

padBoth(string|null $source, int $size, string $pad) : 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).

Tags
throws
InvalidArgumentException

If the padding string is empty or invalid UTF-8.

example
use function oihana\core\strings\padBoth;

echo padBoth('hello', 11, 'ab');    // Outputs: "abhelloabab"
echo padBoth(null, 6, '*');         // Outputs: "******"
echo padBoth('test', 10, '☺');      // Outputs: "☺☺☺test☺☺☺"
echo padBoth('emoji', 9, '🚀');     // Outputs: "🚀🚀emoji🚀"
echo padBoth('pad', 3, '-');        // Outputs: "pad" (size <= string length, no padding)
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
string

The string padded on both sides.


        
On this page

Search results