Specifications

component parts. PHP provides several string functions (and one regular expression function)
that allow us to do this.
In our example, Bob wants any customer feedback from bigcustomer.com to go directly to
him, so we will split the email address the customer typed in into parts to find out if they work
for Bobs big customer.
Using explode(), implode(), and join()
The first function we could use for this purpose, explode(), has the following prototype:
array explode(string separator, string input);
This function takes a string input and splits it into pieces on a specified separator string. The
pieces are returned in an array.
To get the domain name from the customers email address in our script, we can use the fol-
lowing code:
$email_array = explode(“@”, $email);
This call to explode() splits the customers email address into two parts: the username, which
is stored in $email_array[0], and the domain name, which is stored in $email_array[1].
Now we can test the domain name to determine the customers origin, and then send their feed-
back to the appropriate person:
if ($email_array[1]==”bigcustomer.com”)
$toaddress = “bob@bobsdomain.com”;
else
$toaddress = “feedback@bobsdomain.com”;
Note if the domain is capitalized, this will not work. We could avoid this problem by convert-
ing the domain to all uppercase or all lowercase and then checking:
$email_array[1] = strtoupper ($email_array[1]);
You can reverse the effects of explode() using either implode() or join(), which are identi-
cal. For example
$new_email = implode(“@”, $email_array);
This takes the array elements from $email_array and joins them together with the string passed
in the first parameter. The function call is very similar to explode(), but the effect is opposite.
Using strtok()
Unlike explode(), which breaks a string into all its pieces at one time, strtok() gets pieces
(called tokens) from a string one at a time. strtok() is a useful alternative to using explode()
for processing words from a string one at a time.
Using PHP
P
ART I
102
06 7842 CH04 3/6/01 3:41 PM Page 102