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 HostnameLet'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:
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, '', 0, 1); echo $hostname; ?> |
Now, this would output:
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 UsernameNow 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:
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, '', 0, 1);
echo "username: $username<br />\n"; echo "hostname: $hostname<br />\n"; ?> |
Output:
username: someone hostname: somwhere.com |
Next: Manually Stripping Tags >>
More Programming Basics Articles
More By Matt Wade