str pad in PHP

In this article I will explain how the str_pad() function can be used in PHP.
  • 1765

str_pad() function in PHP

The str_pad() function is used to pad a string to a new length.

Syntax

str_pad(string,length,pad_string,pad_type)

Parameter

  • string string is required parameter. it is specify for pad to string.
  • length length is required parameter. it is specify for the new string length. Nothing will be done, If this value is less than the original length of the string.
  • pad_string pad_string is oprional parameter. it is specify for the string to use for padding. Default value of pad_string is whitespace.
  • pad_type pad_type is optional parameter. it is specifies what side to pad the string. possible values:
    • STR_PAD_BOTH - It is pad to both sides of the string. If not an even number, the right side gets the extra padding.
    • STR_PAD_LEFT - It is pad to the left side of the string.
    • STR_PAD_RIGHT - This is the default value, It is pad to the right side of the string.
Example

The following example show to how the str_pad() function can be used in PHP.

<html>

<body>

<h3 style="color: skyblue;">str_pad() function example in PHP</h3>

    <?php

    $string = "C-sharpcorner";

    echo str_pad($string,20,".").'<br/>';

    $string = "C-sharpcorner";

    echo str_pad($string,20,"*",STR_PAD_LEFT).'<br/>';

    $string = "C-sharpcorner";

    echo str_pad($string,20,"_",STR_PAD_BOTH).'<br/>';

    ?>

</body>

</html>

 

Output

str-pad-php.gif
You may also want to read these related articles here
 
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.