PHP Strings Primer - Padding strings
(Page 15 of 37 )
The ability to pad strings allows us to provide consistent output no matter if the strings are of varying lengths. To illustrate the use of padding strings, let's create a receipt.
Normally when you look at a receipt, the first line contains the store name or similar data. A common practice is to encapsulate the store name within asterisks. Let's assume that we have 30 characters available for us to display the store name. Using 'str_pad()' to display the store name, enclosed by asterisks, would be done as follows.
<?php $storename = 'My Store'; echo str_pad($storename, 30, '*', STR_PAD_BOTH) . "<br />\n"; ?> |
The output from this code would be:
***********My Store*********** |
As you can see, asterisks were added to both sides of the store name, and the resulting string ends up being 30 characters total. The first three parameters to 'str_pad()' are fairly self explanatory. The first is the string to pad, the second is the length to pad it to, and finally the third is the character, or characters, to pad it with. Of special note is that the third parameter is optional. If it is not specified, the string is padded with spaces.
However, the fourth (optional) parameter could use a little explanation. There are three possible values we can use for this parameter.
STR_PAD_RIGHT -Using this mode, the padding will be added to the right of the string. This is the default mode of operation for 'str_pad()'. If the fourth parameter is not specified, it will default to this.STR_PAD_LEFT -This mode will place the padding on the left of the string.STR_PAD_BOTH -Both sides of a string will be padded when this mode is used.
The next portion of our receipt will contain items and their prices. The items should be flush against our left margin and the prices against the right. This is another perfect opportunity to use the 'str_pad()' function. Let's see how we would express what we want in code.
<?php echo str_pad('Socks', 10, '_', STR_PAD_RIGHT) . Â Â Â Â str_pad('5.99', 20, '_', STR_PAD_LEFT) . Â Â Â Â "<br />\n"; echo str_pad('Trousers', 10, '_', STR_PAD_RIGHT) . Â Â Â Â str_pad('12.99', 20, '_', STR_PAD_LEFT) . Â Â Â Â "<br />\n"; ?> |
The output from this code would be:
Socks_____________________5.99Trousers_________________12.99
As you can see from the output above, it doesn't line up as we hoped it would! Don't worry, 'str_pad()' did its job. It is just the font used to display the text that is causing it to look uneven. The font used was a variable width font. This means that each character can be a different width. This will cause display problems if we expect things to line up as we did above. In order to correct his, we can force the font to a fixed width and we will see the expected results. To force the display to a fixed width, we will use the '<pre>' tag. It will tell the browser to use a fixed width font.
<?php echo "<pre>\n"; echo str_pad('Socks', 10, '_', STR_PAD_RIGHT) . Â Â Â Â str_pad('5.99', 20, '_', STR_PAD_LEFT) . "\n"; echo str_pad('Trousers', 10, '_', STR_PAD_RIGHT) . Â Â Â Â str_pad('12.99', 20, '_', STR_PAD_LEFT) . "\n"; echo "</pre>\n"; ?> |
Now, our results will look like:
Socks_____________________5.99 Trousers_________________12.99 |
Now, that's more like we expected. You can see that in order to achieve the desired formatting, we used the other two modes of 'str_pad() -left and right padding. You may also notice that we removed the '<br />' tags from the output. When using the '<pre>' tag, a breaking return is not necessary as the newlines will be displayed by the browser.
Next: Multiple Lines >>
More Programming Basics Articles
More By Matt Wade