Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP str_pad() Function

PHP str_pad() Function

PHP String Reference Manual PHP String Reference Manual

Example

Pad the right side of a string to a new length of 20 characters:

Run Example »


Definition and Usage

The str_pad() function pads a string to a new length.


Syntax

str_pad(

string,length,pad_string,pad_type

)

Parameters Description
string Required. Specifies the string to pad.
length Required. Specifies the new string length. If this value is less than the original string length, no action is taken.
pad_string Optional. Specifies the string used for padding. Default is blank.
pad_type Optional. Specifies which side of the string to pad.

Possible values:

  • STR_PAD_BOTH - Pad both sides of the string. If not even, the right side gets the extra padding.
  • STR_PAD_LEFT - Pad the left side of the string.
  • STR_PAD_RIGHT - Pad the right side of the string. This is the default.
  • |

    Technical Details

    Return Value: Returns the padded string.
    PHP Version: 4.0.1+

    More examples

    Example 1

    Pad the left side of a string:

    Run Example »

    Example 2

    Pad both sides of a string:

    The output is:

    .:.:Hello World.:.:.


    PHP String Reference Manual PHP String Reference Manual