Php Tutorial explode () function
Php string function
Definition and usage
The explode () function separates strings into arrays.
Syntax
Explode (separator, string, limit) parameter description
Separator is required. Specifies where to split the string.
String is required. The string to be split.
Limit is optional. Specifies the maximum number of returned array elements.
Example
In this example, we split the string into an array:
| The code is as follows: |
Copy code |
<? Php
$ Str = "hello world. it's a beautiful day .";
Print_r (explode ("", $ str ));
?> |
Output:
| The code is as follows: |
Copy code |
Array
(
[0] => hello
[1] => world.
[2] => it's
[3] =>
[4] => beautiful
[5] => day.
) |
Str_split split function
Definition and usage
The str_split () function separates strings into arrays.
Syntax
Str_split (string, length) parameter description
String is required. Specifies the string to be split.
Length is optional. Specifies the length of each array element. The default value is 1.
Description
If the length is less than 1, the str_split () function returns false.
If the length is greater than the length of the string, the entire string is returned as a unique element of the array.
Example
Example 1
| The code is as follows: |
Copy code |
<? Php
Print_r (str_split ("hello "));
?> |
Output:
| The code is as follows: |
Copy code |
Array
(
[0] => h
[1] => e
[2] => l
[3] => l
[4] => o
) |
Example 2
| The code is as follows: |
Copy code |
<? Php
Print_r (str_split ("hello", 3 ));
?> |
Output:
| The code is as follows: |
Copy code |
Array
(
[0] => El
[1] |
Preg_split -- use a regular expression to split a string
Description
Array preg_split (string pattern, string subject [, int limit [, int flags])
Returns an array that contains the substrings separated by subject along the boundary matching pattern.
If the limit parameter is specified, a maximum of limit substrings are returned. If the limit parameter is-1, there is no limit. It can be used to specify the optional parameter flags.
Flags can be any combination of the following tags (using bitwise OR operator | combination ):
Preg_split_no_empty
If this flag is set, preg_split () returns only non-null components.