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


       · 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 5 hosted by Hostway