How to use the php explode() function

怪我咯
Release: 2023-03-07 20:36:02
Original
5769 people have browsed it

To split a string in php, we can use the function explode(). Usually in development projects, we want to view various parts of the string submitted by the user through a form or other methods, so as to facilitate classification storage and use. For example, looking at words in a sentence, or splitting a URL or email address into its component parts. At this time we can use the explode() function. This article will introduce how to use the php explode() function

In the PHP development manual, Its function prototype is as follows:

array explode(string separator,string input [, int limit]);
Copy after login

This function returns an array composed of strings. Each element is a substring of string, and they are separated by string separator as boundary points. If the limit parameter is set, the returned array contains up to limit elements, and the last element will contain the remainder of the string.

If separator is an empty string (""), explode() will return FALSE. If separator contains a value that is not found in string, explode() returns an array containing a single element of string. If the limit argument is negative, all but the last -limit elements are returned. This feature is new in PHP 5.1.0. For historical reasons, while implode() can accept both argument orders, explode() cannot. You must ensure that the separator parameter comes before the string parameter.

To obtain a domain name from a customer's email address in our PHP project, you can use the PHP script as shown below:

$email_array = explode('@', $email);
Copy after login

Code Description: Here, by calling explode() Function, split the customer's email address into two parts: the user name, which is stored in the first element of the array, which is $email_array[0], and the email domain name is stored in the second array element $email_array[ 1] in. Now, we can test the domain name to determine where the user came from, and then save them to the specified location:

if ($email_array[1]== "qq.com"){
$toaddress= "boss@qq.com";
} else{
$toaddress= "feedback@example.com";
}
Copy after login

Please note that this function will not work if the domain name is uppercase or mixed case works normally. You can avoid this problem by converting the domain name (converting it to all uppercase or lowercase), and then check whether it matches normally as follows:

if (strtolower($email_array[1])== "qq.com"){
$toaddress= "boss@qq.com";
} else{
$toaddress= feedback@example.com;
}
Copy after login

Let’s look at an example of splitting a string. , the code is as follows:

<?php
header("content-type:text/html;charset=utf-8");
$this_year = 2017;
$text = <<< EOT
小李,F,1994,合肥,PHP程序员
小刘,M,1993,安庆,php工程师
小王,F,1991,六安,项目经理
EOT;
$lines = explode("\n", $text);    //将多行数据分开
foreach ($lines as $userinfo) {
    $info = explode(",", $userinfo, 3);  //仅分割前三个数据
    $name = $info[0];
    $sex = ($info[1] == "F")? "女" : "男";
    $age = $this_year - $info[2];
    echo "姓名: $name $sex . 年龄:$age <br/>";
}
?>
Copy after login

Code running results:

How to use the php explode() function

[Related article recommendations]

1.php explode() Detailed explanation of function examples

The above is the detailed content of How to use the php explode() function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template