Programming Basics

  Home arrow Programming Basics arrow Page 22 - 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 - Generating Statistics


    (Page 22 of 37 )

    It is common to see statistics dealing with the text you are working on in many word processors. With PHP, we can emulate these types of statistics. Imagine, if you will, a system where users enter in some text into a form, submit it, and we generate statistics about the character usage and number of words used. Using the three functions we introduced in the beginning of this section, this can easily be accomplished.

    The following is a simple example of how we can achieve that task. Rather than accepting the text from a form, we have assigned it to a variable for simplification purposes.

    <?php
    $text 
    'We hold these Truths to be self-evident,' .
            ' that all Men are created equal';

    /* Find the length of the string */

    $length strlen ($text);

    /* Find the frequency of each character */

    $frequency count_chars ($text1);

    /* Now loop through each character and display
       its frequency and percentage of total */

    foreach($frequency as $key=&gt;$value) {
        printf ("The character '%s' appeared %d times, or %.2f " .
                "percent of the total.&lt;br /&gt;\n",
                chr ($key), $value, ($value $length 100));
    }

    /* display the amount of words in the text */

    echo "&lt;br /&gt;\nThere are " str_word_count($text) .
         " words in this text.&lt;br /&gt;\n";
    ?>

    In this example, we have used a different mode for 'count_chars()' than in our previous example. With this mode, we are returned an array with the ASCII values of the characters as the key. You will notice that we used the 'chr()' function to turn that ASCII value into its normal character representation.

    As noted earlier, the 'str_word_count()' function is only available in PHP version 4.3.0 and newer. Attempting to use this function in earlier versions will result in an undefined function error.

    Without a doubt, you will find many other uses for these three functions. In fact, you will definitely see the use of the 'strlen()' function in many different situations.

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