Programming Basics
  Home arrow Programming Basics arrow Page 15 - PHP Strings Primer
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Dedicated Servers  
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PROGRAMMING BASICS

PHP Strings Primer
By: Matt Wade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    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

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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


       · comment
       · really goood work ,it covers all the major string functions.also explained with...
       · test'ng
     

  • PROGRAMMING BASICS ARTICLES

    - Loops and PHP Decision Making
    - Operators, Conditionals, and PHP Decision-Ma...
    - PHP Decision-Making
    - Coding
    - Server Statistics
    - Looping in PHP
    - Cookies in PHP
    - Working with text files
    - Beginning Object Oriented Programming in PHP
    - A Tour of Decision Making Structures in PHP
    - PHP Strings Primer
    - PHP Control Structures
    - Intro to Vim
    - Reading Directorys with PHP
    - An Overview of Arrays in PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway