SQL PHP HTML ASP JavaScript articles and free scripts to download
 

String str_pad function

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. We can even specify which side of the string the padding is to be done. Let us first see the syntax of str_pad function

str_pad ( input_string, length, pad_string, pad_type)


The last two arguments are optional. If the length specified is less than the input string length the input string is returned. If pad_type is returned then accordingly the string is padded at left, right or both sides. The pad_type can have values like

STR_PAD_LEFT
STR_PAD_RIGHT or
STR_PAD_BOTH


By default it is set to STR_PAD_RIGHT.

Let us try some examples

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**


This function is useful when some data is to be formatted upto some length. We want the blank space after the input is to be blanked 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=split(" ",$str);
while (list ($key, $val) = each ($ar)) {
echo str_pad($val,15,"-",STR_PAD_RIGHT);
echo "<br>";
}

Discuss this tutorial at forum


PHP String Functions
All String Functions in PHP
str_replace: How to replace a part of a string with another string
str_ireplace: Case in-sensitive search and replace using array of strings
strlen: How to find length of a string in PHP?
trim: Removing empty space from both sides of a string
strrev: Reversing a string by using strrev function in PHP
Adding two or more strings in PHP
stristr: Searching for a presence of a string inside another string
nl2br: Adding Line breaks inside a string in place of carriage returns
split: Breaking a string to form array using delimiters
substr: Collecting part of a string
substr_count: Counting occurrence of sub string in main string
str_repeat: Repeating a string number of times
strtolower(): function to change alphabets to Lower case
strtoupper(): function to change characters to upper case letters
function to collect part of a string with two landmarks
Random string generator with number and alphabets for password
strcasecmp(): Case insensitive string comparison
strcmp(): Case sensitive string comparison
str_pad(): String pad with specified length
md5 hash of a string: encryption of a string
Separating domain and userid part from an email address using split
htmlspecialchars: Printing html special chars to the page
strip_tags: Removing html tags within a string
ucwords: Removing html tags within a string



 

Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript
PHP Tutorial Index
String Functions
htmlspecialchars
md5 hash
nl2br
Random string
strip_tags
str_replace
str_ireplace
strlen
strrev
stristr
split
substr
substr_count
str_repeat
strtolower
strcasecmp
strcmp
str_pad
trim
ucwords
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
PHP Tutorials
Date & Time
Array
String Functions
Math Functions
Form Handling
File Handling
Comment Posting
Content Management
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.