Programming Basics

  Home arrow Programming Basics arrow Page 13 - 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 - Capitalization


    (Page 13 of 37 )

    The two functions, 'ucfirst()' and 'ucwords()', provide very similar functionality. Both take a string and apply some level of capitalization to it. The difference is that 'ucfirst()' only capitalizes the first character of a string, while 'ucwords()' capitalizes the first character of every word in a string. Both of these functions are generally used for display purposes only.

    Using the 'ucfirst()' function can come in handy when you have a string that you want to display, and you want to make sure that the first character is capitalized. It is a very simple function to use, as shown by this example.

    <?php
    $str 
    'this is a sentence.';
    echo 
    ucfirst ($str);
    ?>

    This would output:

    This is a sentence.

    It is worth noting that the ucfirst() function only touches the first character in a string. If a string has capital letters in it, they will remain unmodified. Let's look at an example to illustrate this point.

    <?php
    $str 
    'this Sentence HAS some other letters capitalized.';
    echo 
    ucfirst ($str);
    ?>

    Output:

    This Sentence HAS some other letters capitalized.

    The 'ucwords()' function is frequently used to properly capitalize names, titles, and headlines. It is worth noting that this function is far from perfect however. In order to demonstrate the shortcoming of the 'ucwords' function, let us look at some examples. Each example will be followed immediately by its actual output and hoped for output.

    <?php
    echo ucwords ('of mice and men');
    ?>

    Actual:

    Of Mice And Men

    Wanted:

    Of Mice and Men

    <?php
    echo ucwords ('john o'conner');
    ?>

    Actual:

    John O'conner

    Wanted:

    John O'Conner

    <?php
    echo ucwords ('TOM SAWYER');
    ?>

    Actual:

    TOM SAWYER

    Wanted:

    Tom Sawyer

    This last example is easily corrected by using the 'strtolower' function.

    <?php
    echo ucwords (strtolower ('TOM SAWYER'));
    ?>

    The other examples are just common pitfalls of the ucwords function. Other than designing your own function to capitalize the words correctly, there is no way to fix the problem.

    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 3 - Follow our Sitemap