PHP string str_pad() Function

The str_pad() is in-built PHP function which is use to pad a string to a certain length. It returns input string padded on the left, right both sides to the specified function.

Syntax:

ParameterDescriptionRequired/Optional
StringSpecify the string.Required
LengthSpecify the new string length.Required
Pad_stringSpecify the string for padding.Optional
Pad_typeSpecify what side to pad the string.
Following Value:
  • STR_PAD_BOTH
  • STR_PAD_LEFT
  • STR_PAD_RIGHT
Optional

Note 1: The pad-string may be reduced if the padding character can't be divided by the pad_string's length.

Note 2: If the pad_type is not specified then it assumed as STR_PAD_RIGHT.

Example 1

Output:

Your string isHello Javatpoint
By using str_pad() function;Hello Javatpoint.....

Example 2

Output:

Your string is:Javatpoint
By using str_pad() function:Javatpoint

Example 3

Output:

Your string isHello Javatpoint
By using str_pad() function:.....Hello Javatpoint

Example 4

Output:

Your string is: Javatpoint
By using str_pad() function: Javatpoint............

Example 5

Output:

Your string isHello Javatpoint
By using str_pad() function:..Hello Javatpoint..

Next TopicPHP String




Latest Courses