PHP str_pad() Function


The str_pad() function is used to pads a string to a new length.it returns string padded on the left,right and both sides to the specified function.

Syntax
str_pad(string,length,pad_string,pad_type)
Parameters
  • string - (Required) Specify the string to length.
  • length - (Required) Specify the new string to length.
  • pad_string - (optional) Specifies the string for padding.
  • pad_type - (optional) Specifies the which side to pad the string.
  • pad_type - STR_PAD_BOTH,STR_PAD_LEFT,STR_PAD_RIGHT
Example
<?php
    $a = "Good Morning";
    echo str_pad($a,30,".");
?>

Output

Good Morning..................
Example
<?php
    $a = "Good Morning";
    echo str_pad($a,20,".",STR_PAD_LEFT)."<br><br>";
    $b = "Good Morning";
    echo str_pad($b,20,".:",STR_PAD_BOTH);
?>

Output

........Good Morning

.:.:Good Morning.:.:

Prev Next