Hudzilla.org - the homepage of Paul Hudson
Contents > Functions > Playing with strings Wish List | Report Bug | About Me ]

4.7.16     Padding out a string: str_pad()

This is NOT the latest copy of this book; click here for the latest version.

string str_pad ( string input, int pad_length [, string pad_string [, int pad_type]])

Next up, str_pad() makes a given string (parameter one) larger by X number of characters (parameter two) by adding on spaces. For example:

<?php
    $string
= "Goodbye, Perl!";
    
$newstring = str_pad($string, 10);
?>

That code would leave " Goodbye, Perl! " in $newstring, which is the same string from $string except with five spaces on either side, equalling the 10 we passed in as parameter two.

Str_pad() has an optional third parameter that lets you set the padding character to use, so:

<?php
    $string
= "Goodbye, Perl!";>
    
$newstring = str_pad($string, 10, 'a');
?>

That would put "aaaaaGoodbye, Perl!aaaaa" into $newstring.

We can extend the function even more by using it is optional fourth parameter, which allows us to specify which side we want the padding added to. The fourth parameter is specified as a constant, and you either use STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH:

<?php
    $string
= "Goodbye, Perl!";
    
$a = str_pad($string, 10, '-', STR_PAD_LEFT);
    
$b = str_pad($string, 10, '-', STR_PAD_RIGHT);
    
$c = str_pad($string, 10, '-', STR_PAD_BOTH);
?>

That code will set $a to be "----------Goodbye, Perl!", $b to be "Goodbye, Perl!----------", and $c to be "-----Goodbye, Perl!-----", as expected.

Note that HTML only allows a maximum of one space at any time. If you want to pad more, you will need to use "&nbsp;", the HTML code for non-breaking space.





<< 4.7.15 Comparing strings: strcmp() and strcasecmp()   4.7.17 Complex string printing: printf() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
hobot - 16 Oct 2008

The problem some of you are having...this chapter is incorrect, at least for PHP 4.

It will only pad if the second paramater is more than the number of characters in the strings. So to add 10 -'s to the a string, you could use the code...

<?php

$foo = "Goodbye, Perl!";

$foo = str_pad($foo, strlen($foo)+10, '-', str_pad_left);

print $foo ?>

Tom - 16 Oct 2008

Hmm....none of these functions seem to work for me, all they do is print out the string but doesn't apply any parameters given.

Awesome book though!

Gogo the Great, Kingstown, Serbia - 16 Oct 2008

btw:
It would be really nice if users' comments had been properly dated. At least in my browser (Mozzila Firefox), the date of every user's comment is equal to current date, which at first made me think there's a huge PHP fans' community visiting this site simultaneously with me :o

Gogo the Great - 16 Oct 2008

This online manual wouldn't be as nearly as good without the precious users' comments!

Thank you all!

Unix Programmer - 16 Oct 2008

A couple of things:

1) According to the manual, the default padding is to the right (STR_PAD_RIGHT), at least for PHP 5

2) As referred to in the previous post, the second parameter is the length of the padded string to be created. Padding will be added if this length is greater than the length of the first parameter; otherwise the first string will be copied as is.

M.Asim - 16 Oct 2008

hi it's M.Asim i sorry to say that in this page, that cade is not working

<?php
$string = "Goodbye, Perl!";>
$newstring = str_pad($string, 10, 'a');
print $newstring;
?>
bcs If the value of pad_length is less than the length of the given string then no padding will enter there .

arty - 16 Oct 2008

Padding text in HTML with non-breaking spaces is a bad practice. You should better use CSS to add paddings/margins, or use style="word-wrap: pre;" to see newlines and spaces exactly as in you text editor.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is four plus eight?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow