str_pad() Padding string at both ends

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)
ParametersDESCRIPTION
$input_strRequired : Input string
$lengthRequired : If $length is less than , zero or equal to length of Input string then no padding takes place and $input_string is returned
$pad_stringOptional : To be used as padding. If not given blank space is used. Padding is done upto the limit.
pad_typeOptinoal: Which side padding is to be added
STR_PAD_RIGHT ( default )
STR_PAD_LEFT
STR_PAD_BOTH
Using the string function str_pad() we can pad one input string upto some given length or pad using another string. If no second string is given then the input string is padded by blank space.

This function is useful when some data is to be formatted upto some length. We want the blank space after the input or marked with some chars like ---- . Another example is where we are displaying data in rows and wants all records to perfectly align in left and right. Here if all data are not of equal length we can pad them and keep all in one line. The extra space can be formatted as per requirement.

Here is another example of str_pad function

Welcome--------
to-------------
Plus2net.com---
ASP------------
Section--------
to-------------
learn----------
web------------
programing-----

The code for the above is here

$str="Welcome to Plus2net.com ASP Section to learn web programing";
$ar=explode(" ",$str);
while (list ($key, $val) = each ($ar)) {
echo str_pad($val,15,"-",STR_PAD_RIGHT);
echo "<br>";
}

explode() Break the string by using delimiter
String Functions Trim the string from both ends Change all alphabetic chars to Upper case Checking all lower case or upper case chars in a string
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer