Jump to: navigation, search

Php str pad

From w3cyberlearnings

Contents

PHP function str_pad

This function pads a given string to a new length.

Syntax str_pad

  • String: string to pad
  • length: new string length
  • padstring: string uses for padding, and default is whitespace.
  • padtype: specifies the side of the pad (STR_PAD_BOTH: Both side), (STR_PAD_RIGHT: Right side),(STR_PAD_LEFT: Left side)
str_pad(string,length, padstring, padtype);

Example 1:DEFAULT

<?php

$str ="Up to you";
$new_str = str_pad($str,"20","-");
echo $new_str.'last';

?>

Output

Up to you-----------last

Example 2: STR_PAD_BOTH

<?php

$str ="Up to you";
$new_str = str_pad($str,"20","-",STR_PAD_BOTH);
echo $new_str;

?>


Output

-----Up to you------

Example 3:STR_PAD_RIGHT

<?php

$str ="Up to you";
$new_str = str_pad($str,"20","-",STR_PAD_RIGHT);
echo $new_str;

?>

Output

Up to you-----------

Example 4: STR_PAD_LEFT

<?php

$str ="Up to you";
$new_str = str_pad($str,"20","-",STR_PAD_LEFT);
echo $new_str;

?>

Output

 -----------Up to you

Example 5

<?php
$input = "Great";
echo str_pad($input, 10);                      // produces "Great     "
echo '<br/>';
echo str_pad($input, 10, "-=", STR_PAD_LEFT);  // produces "-=-=-Great"
echo '<br/>';
echo str_pad($input, 10, "_", STR_PAD_BOTH);   // produces "__Great___"
echo '<br/>';
echo str_pad($input, 6 , "___");               // produces "Great_"
?>

Output

Great 
-=-=-Great
__Great___
Great_

Related Functions


Navigation
Web
SQL
MISC
References