str_pad
PADS a STRING to a certain length with another STRING.
If the value of $pad_length < 0 or $pad_length ≤ $input, no padding takes place, and $input will be returned.
The $pad_string may be truncated if the required number of padding characters can't be evenly divided by the $pad_string's length.
<?php
str str_pad ( str $input , int $pad_length [,
str $pad_string = " " [,
int $pad_type = STR_PAD_RIGHT ]] )
where,
$input = The input STRING
$pad_length = The lenght to be reached
$pad_string = The STRING to be padded
$pad_type = To control de pad type
( SEE the below TABLE )
?>
$input
The input STRING.
$pad_length
The lenght to be reached.
$pad_string
The STRING to be padded.
$pad_type
CONSTANT |
MEANING |
STR_PAD_RIGHT |
The pad will be made at right |
STR_PAD_LEFT |
The pad will be made at left |
STR_PAD_BOTH |
The pad will be made at left and right |
ed48 |
EXERCISE
<?php
$stri01 = "It's cold in the winter";
$strp01 = '-+-*-+-*-+-*-+-*-';
$nbrf01 = 5;
$strfL = str_pad($stri01, $nbrf01, $strp01, STR_PAD_LEFT);
echo $strfL;
$strfR = str_pad($stri01, $nbrf01, $strp01, STR_PAD_RIGHT);
echo '<br>' . $strfR;
$strfB = str_pad($stri01, $nbrf01, $strp01, STR_PAD_BOTH);
echo '<br>' . $strfB;
?>
RESULT
It's cold in the winter
23 characters
-+-*-+-*-+-*-+-*-
17 characters
Number of characters to reach
5 characters
Because the number of characters to reach is less than the STRING length, in both associations nothing will change.
It's cold in the winter
It's cold in the winter
It's cold in the winter
The same would be obtained if the number of characters to be achieved is equal to the string test.
EXERCISE
<?php
$stri02 = "It's cold in the winter";
$strp02 = '-+-*-+-*-+-*-+-*-';
$nbrf02 = 28;
$strfL = str_pad($stri02, $nbrf02, $strp02, STR_PAD_LEFT);
echo $strfL;
$strfR = str_pad($stri02, $nbrf02, $strp02, STR_PAD_RIGHT);
echo '<br>' . $strfR;
$strfB = str_pad($stri02, $nbrf02, $strp02, STR_PAD_BOTH);
echo '<br>' . $strfB;
?>
RESULT
It's cold in the winter
23 characters
-+-*-+-*-+-*-+-*-
17 characters
Number of characters to reach
5 characters
Since the number of characters to reach is greater than the length of input STRING, in both associations there will be changes.
-+-*-It's cold in the winter
It's cold in the winter-+-*-
-+It's cold in the winter-+-
Note that the input STRING has fewer characters than expected to be reached.
EXERCISE
<?php
$stri03 = "It's cold in the winter";
$strp03 = '-';
$nbrf03 = 28;
$strfL = str_pad($stri03, $nbrf03, $strp03, STR_PAD_LEFT);
echo $strfL;
$strfR = str_pad($stri03, $nbrf03, $strp03, STR_PAD_RIGHT);
echo '<br>' . $strfR;
$strfB = str_pad($stri03, $nbrf03, $strp03, STR_PAD_BOTH);
echo '<br>' . $strfB;
?>
EXERCISE
<?php
/* Pad a string to a certain length
with another string */
echo "Basic operations.<br>";
$input_string = "str_pad()";
$pad_length = 20;
$pad_string = "-+";
var_dump( str_pad($input_string, $pad_length) );
// default pad_string & pad_type
echo '<br><br>';
var_dump( str_pad($input_string, $pad_length,
$pad_string) );
// default pad_type
echo '<br><br>';
var_dump( str_pad($input_string, $pad_length,
$pad_string, STR_PAD_LEFT) );
echo '<br><br>';
var_dump( str_pad($input_string, $pad_length,
$pad_string, STR_PAD_RIGHT) );
echo '<br><br>';
var_dump( str_pad($input_string, $pad_length,
$pad_string, STR_PAD_BOTH) );
echo '<br><br>';
echo "<br><br>variations with input string and pad-length:<br>";
/* different input string variation */
$input_strings = array(
"variation", // normal string
"", // empty string
NULL, // NULL
true, // boolean
15, // numeric
15.55, // numeric
"2990" // numeric string
);
/* different pad_lengths */
$pad_lengths = array(
-PHP_INT_MAX, // huge negative value
-1, // negative value
0, // pad_length < sizeof(input_string)
9, // pad_length <= sizeof(input_string)
10, // pad_length > sizeof(input_string)
16, // pad_length > sizeof(input_string)
);
$pad_string = "=";
/* loop through to use each variant of
$pad_length on each element of
$input_strings array */
foreach ($input_strings as $input_string ) {
foreach ($pad_lengths as $pad_length ) {
var_dump( str_pad($input_string, $pad_length) );
// default pad_string & pad_type
echo '<br><br>';
var_dump( str_pad($input_string, $pad_length,
$pad_string) );
// default pad_type
echo '<br><br>';
var_dump( str_pad($input_string, $pad_length,
$pad_string, STR_PAD_LEFT) );
echo '<br><br>';
var_dump( str_pad($input_string, $pad_length,
$pad_string, STR_PAD_RIGHT) );
echo '<br><br>';
var_dump( str_pad($input_string, $pad_length,
$pad_string, STR_PAD_BOTH) );
echo '<br><br>';
}
}
echo "<br><br>variation with pad string:<br>";
$pad_strings = array ("=", 1, true, "string_pad", 1.5, "\t", '\t');
$input_string="variation";
$pad_length = 16;
foreach ( $pad_strings as $pad_string ) {
var_dump( str_pad($input_string, $pad_length,
$pad_string) );
// default pad_type
echo '<br><br>';
var_dump( str_pad($input_string, $pad_length,
$pad_string, STR_PAD_LEFT) );
echo '<br><br>';
var_dump( str_pad($input_string, $pad_length,
$pad_string, STR_PAD_RIGHT) );
echo '<br><br>';
var_dump( str_pad($input_string, $pad_length,
$pad_string, STR_PAD_BOTH) );
echo '<br><br>';
}
echo "<br><br>error conditions.<br>";
echo "<br><br>padding string as null:<br>";
try {
str_pad($input_string, 12, NULL);
} catch (\ValueError $e) {
echo $e->getMessage() . "<br>";
}
try {
str_pad($input_string, 12, "");
} catch (\ValueError $e) {
echo $e->getMessage() . "<br>";
}
/* bad pad_type - passing an undefined one */
try {
str_pad($input_string, $pad_length, "+", 15);
} catch (\ValueError $e) {
echo $e->getMessage() . "<br>";
}
?>
EXERCISE
<?php
if (getenv("USE_ZEND_ALLOC") === "0") {
die("skip Zend MM disabled");
}
/* Test str_pad() function:
* with unexpected inputs for '$pad_length'
* and expected type for '$input'
*/
echo "Testing str_pad() function: with large
value for for 'pad_length' argument.<br>";
//defining '$input' argument
$input = "Test string";
$extra_large_pad_length = PHP_INT_MAX*5;
try {
var_dump( str_pad($input, $extra_large_pad_length) );
} catch (\TypeError $e) {
echo $e->getMessage() . "<br>";
}
$php_int_max_pad_length = PHP_INT_MAX;
var_dump( str_pad($input, $php_int_max_pad_length) );
?>
EXERCISE
<?php
// Split from str_pad for NUL Bytes
// 7-bit ASCII
$string = chr(0).chr(255).chr(128).chr(234).chr(143);
/* different pad_lengths */
$pad_lengths = [
-PHP_INT_MAX, // huge negative value
-1, // negative value
0, // pad_length < sizeof(input_string)
9, // pad_length <= sizeof(input_string)
10, // pad_length > sizeof(input_string)
16, // pad_length > sizeof(input_string)
];
$pad_string = "=";
/*loop through to use each variant of $pad_length on
each element of $input_strings array */
foreach ($pad_lengths as $pad_length ) {
// default pad_string & pad_type
var_dump( bin2hex( str_pad($string, $pad_length) ) );
// default pad_type
echo '<br><br>';
var_dump( bin2hex( str_pad($string, $pad_length,
$pad_string) ) );
echo '<br><br>';
var_dump( bin2hex( str_pad($string, $pad_length,
$pad_string, STR_PAD_LEFT) ) );
echo '<br><br>';
var_dump( bin2hex( str_pad($string, $pad_length,
$pad_string, STR_PAD_RIGHT) ) );
echo '<br><br>';
var_dump( bin2hex( str_pad($string, $pad_length,
$pad_string, STR_PAD_BOTH) ) );
}
?>
EXERCISE
<?php
if (PHP_INT_SIZE == 4)
die("skip this test is for 32bit platform only");
if (getenv("USE_ZEND_ALLOC") === "0") {
die("skip Zend MM disabled");
}
/* Test str_pad() function: with
* unexpected inputs for '$pad_length'
* and expected type for '$input'
*/
echo "Testing str_pad() function:
with large value for for 'pad_length' argument:<br>";
//defining '$input' argument
$input = "Test string";
$pad_length = PHP_INT_MAX - 16;
/* zend_string header is 16 bytes */
var_dump( str_pad($input, $pad_length) );
?>