• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Matt Doyle | Elated Communications

Web and WordPress Development

  • About Me
  • Blog
    • Design & Multimedia
      • Photoshop
      • Paint Shop Pro
      • Video & Audio
    • Online Marketing
      • E-Commerce
      • Social Media
    • Running a Website
      • WordPress
      • Apache
      • UNIX and Linux
      • Using FTP
    • Web Development
      • HTML
      • CSS
      • JavaScript
      • PHP
      • Perl and CGI Scripting
  • Portfolio
  • Contact Me
  • Hire Me
Home / Blog / Web Development / PHP / PHP Strings / Padding PHP Strings with str_pad()

Padding PHP Strings with str_pad()

3 December 2009 / 1 Comment

In Trimming PHP Strings you learned how to remove unwanted whitespace characters from around a string. The opposite of trimming is padding — adding extra characters to either end (or both ends) of a string to make the string a certain length.

Padding is handy for lining up strings vertically for display, for adding leading zeroes to numbers, or for any situation where you need a string to be of a certain fixed length.

In this article you’ll explore PHP’s str_pad() function for padding strings.

Another way to pad strings is to use PHP’s printf() and sprintf() functions.

Basic str_pad() usage

In its basic form, you pass 2 arguments to str_pad(): the string to pad, and how long you want the string to be after it’s been padded. str_pad() then returns the string padded with spaces on the right-hand side. For example:


$myString = "Hello there";

// Displays "|Hello there         |"
echo '|' . str_pad( $myString, 20 ) . '|';

If you specify a final length that’s shorter than the string to pad then the string is untouched:


$myString = "Hello there";

// Displays "|Hello there|"
echo '|' . str_pad( $myString, 5 ) . '|';

Padding with characters other than spaces

If you want to pad using another character, pass that character as the 3rd argument to str_pad():


$myString = "Hello there";

// Displays "|Hello there#########|"
echo '|' . str_pad( $myString, 20, '#' ) . '|';

You can even pass a string of characters to use as padding. str_pad() will repeat and/or truncate the string as necessary:


$myString = "Hello there";

// Displays "|Hello there123412341|"
echo '|' . str_pad( $myString, 20, '1234' ) . '|';

Padding on the left

To pad the string on the left instead of on the right, pass the flag STR_PAD_LEFT as the 4th argument to str_pad(). (The default value for this flag is STR_PAD_RIGHT.) For example:


$myString = "Hello there";

// Displays "|         Hello there|"
echo '|' . str_pad( $myString, 20, ' ', STR_PAD_LEFT ) . '|';

Padding on both sides

You can also pad a string on both sides by passing the flag STR_PAD_BOTH as the 4th argument. str_pad() attempts to add padding as evenly as possible to both sides of the string:


$myString = "Hello there";

// Displays "|    Hello there     |"
echo '|' . str_pad( $myString, 20, ' ', STR_PAD_BOTH ) . '|';

You’ve now looked at how to use PHP’s str_pad() function to add padding to a string. You’ve seen how to use custom padding characters, and how to pad a string on the left, on the right, or on both sides.

Happy PHP coding!

Filed Under: PHP Strings Tagged With: both, left, pad a string, padding character, padding strings, php, right, str_pad

Reader Interactions

Comments

  1. Dragos Balaceanu says

    20 May 2021 at 10:40 pm

    a usefull article

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

To include a block of code in your comment, surround it with <pre> ... </pre> tags. You can include smaller code snippets inside some normal text by surrounding them with <code> ... </code> tags.

Allowed tags in comments: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre> .

Primary Sidebar

Hire Matt!

Matt Doyle headshot

Need a little help with your website? I have over 20 years of web development experience under my belt. Let’s chat!

Matt Doyle - Codeable Expert Certificate

Hire Me Today

Call Me: +61 2 8006 0622

Stay in Touch!

Subscribe to get a quick email whenever I add new articles, free goodies, or special offers. I won’t spam you.

Subscribe

Recent Posts

  • Make a Rotatable 3D Product Boxshot with Three.js
  • Speed Up Your WordPress Website: 11 Simple Steps to a Faster Site
  • Reboot!
  • Wordfence Tutorial: How to Keep Your WordPress Site Safe from Hackers
  • How to Make Awesome-Looking Images for Your Website

Footer

Contact Matt

  • Email Me
  • Call Me: +61 2 8006 0622

Follow Matt

  • E-mail
  • Facebook
  • GitHub
  • LinkedIn
  • Twitter

Copyright © 1996-2023 Elated Communications. All rights reserved.
Affiliate Disclaimer | Privacy Policy | Terms of Use | Service T&C | Credits