str_pad() function in PHP || PHP str_pad function
×


str_pad() function in php

2228

PHP str_pad() function is used to pad a string to a new length given by the user. It is an inbuilt function of PHP.

Syntax:

str_pad($string,$length,$pad_string,$padding_type);

here,

  • $string is a PHP string that is a pad by this function. It is mandatory.
  • $length is the length of the resultant string if the value of $length is less than the actual length of the string then this function will not do anything. It is also a mandatory parameter.
  • $pad_string is also a PHP string which uses for padding by this function. It is optional. If it is not given then the whitespace will be used for padding.
  • $padding_type decide on which side to pad the $string. It is optional. It has three possible values:

a. STR_PAD_BOTH: pad both sides (left and right) of the string. In case of odd number length, the right side accepts extra padding.

b. STR_PAD_LEFT: pad left side of the string.

c. STR_PAD_RIGHT: pad right side of the string. By default it is selected.


Example:

<?php
$str="Hello Friends";
$p_string=str_pad($str,16,"!",STR_PAD_RIGHT);
echo "Original string is : <strong>$str</strong><br>";
echo "After padding string is : <strong>$p_string</strong>";
?>

Output:



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments