There are many, many string splitting functions in PHP. Now I will introduce to you various examples of string splitting in PHP. Friends in need can refer to them for reference.
The basic syntax of PHP function split() is
array split ( string $pattern, string $string [, int $limit] ).
Example
The code is as follows |
Copy code |
代码如下 |
复制代码 |
list($user, $pass, $uid, $gid, $extra) = split (":", $passwd_line, 5);
?>
|
list($user, $pass, $uid, $gid, $extra) = split (":", $passwd_line, 5);
?>
|
Parse dates that may be separated by slashes, dots, or dashes:
代码如下 |
复制代码 |
// 分隔符可以是斜线,点,或横线
$date = "04/30/1973";
list($month, $day, $year) = split ('[/.-]', $date);
echo "Month: $month; Day: $day; Year: $year
n";
?>
|
Example 2
The code is as follows |
Copy code |
// The delimiter can be a slash, dot, or horizontal line
$date = "04/30/1973";
list($month, $day, $year) = split ('[/.-]', $date);
echo "Month: $month; Day: $day; Year: $year
n";
?>
|
str_split() function
代码如下 |
复制代码 |
$str = "Hello Friend";
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);
?>
|
If the optional split_length parameter is specified, each element in the returned array is a character block of length split_length, otherwise each character block is a single character.
If split_length is less than 1, return FALSE. If the split_length argument exceeds the length of string , the entire string will be returned as only one element of the array.
代码如下 |
复制代码 |
Array
(
[0] => H
[1] => e
[2] => l
[3] => l
[4] => o
[5] =>
[6] => F
[7] => r
[8] => i
[9] => e
[10] => n
[11] => d
)
Array
(
[0] => Hel
[1] => lo
[2] => Fri
[3] => end
)
|
The code is as follows |
Copy code |
$str = "Hello Friend";
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);
?>
|
The above routine will output:
The code is as follows |
Copy code |
Array
(
[0] => H
[1] => e
[2] => l
[3] => l
[4] => o
[5] =>
[6] => F
[7] => r
[8] => i
[9] => e
[10] => n
[11] => d
)
Array
(
[0] => Hel
[1] => lo
[2] => Fri
[3] => end
)
|
chunk_split() function
string chunk_split ( string $body [, int $chunklen [, string $end ]] )
This function is useful for splitting a string into small pieces. For example, convert the output of base64_encode() into a string that conforms to RFC 2045 semantics. It inserts end (default is " ") after every chunklen (default is 76) characters. This function returns a new string without modifying the original string.
Example #1 chunk_split() Example
The code is as follows |
Copy code |
代码如下 |
复制代码 |
// 使用 RFC 2045 语义格式化 $data
$new_string = chunk_split(base64_encode($data));
?>
|
// Format $data using RFC 2045 semantics
$new_string = chunk_split(base64_encode($data));
?>
|
explode — Split one string into another
代码如下 |
复制代码 |
// 示例 1
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
// 示例 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *
?>
|
Example #1 explode() Example
The code is as follows |
Copy code |
// Example 1
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
// Example 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *
?>
|
http://www.bkjia.com/PHPjc/445279.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445279.htmlTechArticleThere are many, many string splitting functions in php. Let me introduce various characters in php to you. String segmentation examples, friends in need can enter for reference. The base of PHP function split()...