str_pad() extends a string to a target length using spaces or another padding string. Padding can be added on the right, left or both sides.
$label = 'PHP';
echo str_pad($label, 8, '.', STR_PAD_RIGHT); // PHP.....
Start with this current pattern. The examples below are retained, corrected where necessary, and expanded for additional variations and output.
echo str_pad("plus2net.com",15,"*");//output: plus2net.com***
echo str_pad("plus2net.com",15,"*",STR_PAD_LEFT);//output: ***plus2net.com
echo str_pad("plus2net.com",15,"*",STR_PAD_BOTH);//output: *plus2net.com**
Syntax
str_pad ( $input_str, $length, $pad_string, pad_type)
| Parameters | DESCRIPTION |
|---|---|
$input_str | Required : Input string |
$length | Required : If $length is less than , zero or equal to length of Input string then no padding takes place and $input_string is returned |
$pad_string | Optional : To be used as padding. If not given blank space is used. Padding is done upto the limit. |
pad_type | Optinoal: Which side padding is to be added STR_PAD_RIGHT ( default ) STR_PAD_LEFT STR_PAD_BOTH |
$str="Welcome to Plus2net.com ASP Section to learn web programing";
$ar=explode(" ",$str);
foreach ($ar as $key => $val) {
echo str_pad($val,15,"-",STR_PAD_RIGHT);
echo "<br>";
}
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.