How to use the string segmentation function explode ()

Source: Internet
Author: User
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 ";
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.