Programming Basics

  Home arrow Programming Basics arrow Page 27 - 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 - Working with email addresses


    (Page 27 of 37 )

    If you are building a web application, at some time or another you will have to handle email addresses. In this section, we will examine a simple method for breaking email addresses down into their parts -user and host name. We will utilize two different functions to accomplish this, 'strstr()' and 'substr_replace()'.

    The Hostname

    Let's first get the hostname out of the email address. for this, we will use a combination of the 'strstr()' and 'substr_replace()' functions. The 'strstr()' function will return a substring that begins from the character, or characters we search for, to the end of the string. If we search for the '@' character, we should be returned the '@' character plus the host name. Let's look at an example.

    <?php
    $email 
    "someone@somwhere.com";
    $hostname strstr ($email'@');
    echo 
    $hostname;
    ?>

    This would output:

    @somwhere.com

    Now, we just need to worry about removing that leading '@' character. There are many different ways we could accomplish this, but we will be using the substr_replace()' function to do so. This function allows us to specify a range of characters, specified by a start position and length, to replace with whatever string we want. In our case, we will replace the range with no characters in order to remove the first character. Let's see how it is done.

    <?php
    $email 
    "someone@somwhere.com";
    $hostname strstr ($email'@');
    $hostname substr_replace($hostname''01);
    echo 
    $hostname;
    ?>

    Now, this would output:

    somwhere.com

    That is exactly the output we were looking for.

    Let's take a closer look at the 'substr_replace()' function to see how it operates. The first parameter is the string we want to operate on. The second is the replacement string. The third and fourth parameters are the start position and length respectively. Notice that the start position is zero. This is because strings are indexed starting at zero. So the first position is zero, the second is one, and so on.

    The Username

    Now that we have the hostname squared away, let's get the username. Since we would most likely, be separating the username and hostname at the same time, let's just build off of the system we used for the hostname. Remember that at one point we had the hostname in a string, preceded by a '@' character. This is the portion of the email address that we would like to be rid of. Using the str_replace function from earlier in this section would be perfect here. Here's how to do it.

    <?php
    $email 
    "someone@somwhere.com";
    $hostname strstr ($email'@');
    $username str_replace($hostname''$email);
    echo 
    $username;
    ?>

    That would produce the following output:

    someone

    Now that we have the username taken out, let's combine both scripts to see how they work together.

    <?php
    $email 
    "someone@somwhere.com";
    $hostname strstr ($email'@');
    $username str_replace($hostname''$email);
    $hostname substr_replace($hostname''01);

    echo 
    "username: $username&lt;br /&gt;\n";
    echo 
    "hostname: $hostname&lt;br /&gt;\n";
    ?>

    Output:

    username: someone
    hostname: somwhere.com

    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