The basic syntax of the PHP function split () is
Array split (string $ pattern, string $ string [, int $ limit]).
Example
| The code is as follows: |
Copy code |
|
<? Php
List ($ user, $ pass, $ uid, $ gid, $ extra) = split (":", $ passwd_line, 5 );
?> |
Parsing dates that may be separated by diagonal lines, points, or horizontal lines:
Example 2
| The code is as follows: |
Copy code |
|
<? Php
// The delimiter can be a diagonal line, a dot, or a horizontal line.
$ Date = "04/30/1973 ";
List ($ month, $ day, $ year) = split ('[/.-]', $ date );
Echo "Month: $ month; Day: $ day; Year: $ year <br/> n ";
?> |
Str_split () function
If the optional split_length parameter is specified, each element in the returned array is a character block with the length of split_length. Otherwise, each character block is a single character.
If split_length is less than 1, FALSE is returned. If the split_length parameter exceeds the length of the string, the entire string is returned as only one element of the array.
| The code is as follows: |
Copy code |
|
<? Php
$ 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] => El
[1] => lo
[2] => Fri
[3] => end
) |
Chunk_split () function
String chunk_split (string $ body [, int $ chunklen [, string $ end])
It is very useful to use this function to split a string into small pieces. For example, convert the base64_encode () output to a string that complies with the RFC 2045 syntax. It inserts end after each chunklen (76 by default) character ("" by default). This function returns a new string without modifying the original string.
Example #1 chunk_split ()
| The code is as follows: |
Copy code |
|
<? Php
// Format $ data using RFC 2045 syntax
$ New_string = chunk_split (base64_encode ($ data ));
?> |
Explode-use one string to separate another string
Example #1 explode () Example
| The code is as follows: |
Copy code |
|
<? Php
// 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 ;//*
?> |