Friday, October 10, 2008

The str_pad() function

This string could be used to pad a string to a new length.

Syntax: str_pad(string,length[,pad-string,pad-type]);

string – the input string

length – the length of the padded string. If this value is lesser than length of the actual string, no effect will take place

pad-string – the string to be used for padding. If this value is not specified, white spaces will be added as padding.

pad-type –
This parameter can have the following values:

STR_PAD_RIGHT: This is the default value and adds padding to the right of the string.
STR_PAD_LEFT: This value adds padding to the left end of the string.
STR_PAD_BOTH : This value adds padding to both the ends. If the number of characters to be added is not even, the right side is added an extra character.

Example input:

$str1="This is a simple program";
echo str_pad($str1,40,"*",STR_PAD_BOTH);

Example output:

********This is a simple program********

No comments: