Programming Basics

  Home arrow Programming Basics arrow Page 15 - PHP Strings Primer
PROGRAMMING BASICS

PHP Strings Primer
By: Matt Wade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2003-07-11

    Table of Contents:
  • PHP Strings Primer
  • The Basics
  • Single Quotes
  • Double Quotes
  • Heredoc
  • Concatenation
  • Displaying Strings
  • echo
  • print
  • printf
  • Strings Formatting
  • Preparing user input for comparisons
  • Capitalization
  • Reversing strings
  • Padding strings
  • Multiple Lines
  • Data Preparation
  • Adding and Removing Slashes
  • Dealing with HTML Tags and Entities
  • Counting
  • Checking password strength
  • Generating Statistics
  • Substrings (and searching)
  • Extracting Substrings
  • Counting Paragraphs
  • Filtering Words
  • Working with email addresses
  • Manually Stripping Tags
  • Password Strength Revisited
  • Handling URLs and Base64-encoding
  • Parsing URLs
  • Encoding for URLs
  • Encoding for Email
  • Hashing
  • Verifying Integrity
  • User Authentication
  • Conclusion

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    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($storename30'*'STR_PAD_BOTH) . "&lt;br /&gt;\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) .
         "&lt;br /&gt;\n";
    echo 
    str_pad('Trousers'10'_'STR_PAD_RIGHT) .
         str_pad('12.99'20'_'STR_PAD_LEFT) .
         "&lt;br /&gt;\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 "&lt;pre&gt;\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 
    "&lt;/pre&gt;\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.

    More Programming Basics Articles
    More By Matt Wade

    blog comments powered by Disqus
  • PROGRAMMING BASICS ARTICLES

    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP
    - Calendar Construction with PHP
    - PHP`s Calendar Package
    - Getting Modified Versions and Correct Dates ...
    - Combining Date Functions in PHP
    - Using PHP for Date and Time in Programming
    - More Exception Handling with PHP
    - Exception Handling in PHP
    - Error Logging and Handling Exceptions
    - Configuration Directives for Error and Excep...
    - Error and Exception Handling
    - Python Modules for Games


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap