In development projects, we usually want to view the various parts of strings submitted by users through forms or other methods for classified storage and use. For example, you can view the words in a sentence or
In development projects, we usually want to view the various parts of strings submitted by users through forms or other methods for classified storage and use. For example, you can view the words in a sentence, or divide a website or email address into components. Several functions are provided in PHP to implement this requirement. today we will talk about one of the explode () functions.
In the PHP Development Manual, the function prototype is as follows:
Array explode (string separator, string input [, int limit]);
This function returns an array composed of strings. each element is a substring of a string, which is separated by a string separator as a boundary point. If the limit parameter is set, the returned array contains up to limit elements, and the last element contains the rest of the string.
If separator is a null string (""), explode () returns FALSE. If the value of separator cannot be found in string, explode () returns an array containing a single string element. If the limit parameter is negative, all elements except the last-limit element are returned. This feature is added in PHP 5.1.0. For historical reasons, although implode () can receive two parameter sequences, explode () cannot. You must ensure that the separator parameter is prior to the string parameter.
To obtain the domain name through the customer's email address in our PHP project, you can use the following PHP script:
$ Email_array = explode ('@', $ email );
Code description: Here, the customer's email address is divided into two parts by calling The explode () function: user name, which is saved in the first element of the array, that is, $ email_array [0], while the mailbox domain name is saved in the second array element $ email_array [1. Now, we can test the domain name to determine the user's source and save them to the specified location:
If ($ email_array [1] = "qq.com "){
$ Toaddress = "boss@qq.com ";
} Else {
$ Toaddress = "feedback@example.com ";
}
Note that if the domain name is in uppercase or in combination, this function cannot work normally. You can avoid this problem by converting the domain name into uppercase or lowercase letters, and then check whether the matching is normal as follows:
If (strtolower ($ email_array [1]) = "qq.com "){
$ Toaddress = "boss@qq.com ";
} Else {
$ Toaddress = "feedback@example.com ";
}