Programming Basics
  Home arrow Programming Basics arrow Page 13 - 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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
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 / 11
    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 - 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


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

    PROGRAMMING BASICS ARTICLES

    - PHP: Hypertext Preprocessor: What is it?
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT