How to use the PHP explode () function

Source: Internet
Author: User
Tags php explode
To split a string in PHP, we can use the function explode (), usually in our development project, we want to see the various parts of the string that the user submits through the form or other way, so that the classification is stored and used. For example, look at the words in a sentence, or divide a URL or e-mail address into parts. At this point we can use the explode () function, this article will introduce How to use the PHP explode () function

In the PHP development manual, its function prototypes are as follows:

Array explode (string separator,string input [, int limit]);

This function returns an array of strings, each of which is a substring of string, separated by the separator of the strings as the boundary points. If the limit parameter is set, the returned array contains a maximum of limit elements, and the last element will contain the remainder of the string.

If separator is an empty string (""), Explode () returns FALSE. If separator contains a value that is not found in a string, explode () returns an array containing a single element of string. If the limit parameter is a negative number, all elements except the last-limit element are returned. This feature is new in PHP 5.1.0. For historical reasons, although implode () can receive two parameter sequences, explode () does not work. You must ensure that the separator parameter is not preceded by a string parameter.

To get a domain name in our PHP project via the customer's e-mail address, you can use the PHP script shown below:

$email _array = explode (' @ ', $email);

Code Description: Here, by calling the explode () function, the customer's e-mail address is divided into two parts: the user name, which is stored in the first element of the array, that is, $email_array[0], and the mailbox domain name is saved in the second array element $email_array[ 1]. Now, we can test the domain name to determine the source of the user, and then save them to the specified location:

if ($email _array[1]== "qq.com") {$toaddress = "boss@qq.com";} else{$toaddress = "feedback@example.com";}

Note that this function does not work properly if the domain name is capitalized or mixed in case. You can avoid this problem by converting the domain name (converted to uppercase or lowercase), and then follow the following method to check if the match is normal:

if (Strtolower ($email _array[1]) = = "Qq.com") {$toaddress = "boss@qq.com";} else{$toaddress = feedback@example.com;}

Let's look at a segmented string instance with the following code:

<?phpheader ("Content-type:text/html;charset=utf-8"); $this _year = $text = <<< EoT Xiao Li, f,1994, Hefei, PHP programmer Xiao Liu, m,1993, Anqing, PHP engineer Xiao Wang, f,1991, Liu'an, project manager eot; $lines = explode ("\ n", $text);    Separate the multiline data into foreach ($lines as $userinfo) {    $info = explode (",", $userinfo, 3);  Only the first three data are divided    $name = $info [0];    $sex = ($info [1] = = "F")? "Female": "Male";    $age = $this _year-$info [2];    echo "Name: $name $sex. Age: $age <br/> ";}? >

Code Run Result:

Related Article

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.