Use "str_split" to separate strings. The advantage is that a space is also used as an array element. In my previous example, the previous string contains two spaces, and the next string has only one space. However, the output displays the same information. It can also be split by other separators, such as "explode" or "preg_split ",
Use "str_split" to separate strings. The advantage is that a space is also used as an array element. In my previous example, the previous string contains two spaces, and the next string has only one space. However, the output displays the same information.
It can also be split by other separators, such as "explode" or "preg_split ",
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:
$ Str = "hello world. it's a beautiful day .";
Print_r (explode ("", $ str ));
?>
Output:
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
Print_r (str_split ("hello "));
?>
Output:
Array
(
[0] => h
[1] => e
[2] => l
[3] => l
[4] => o
)
Example 2
Print_r (str_split ("hello", 3 ));
?>
Output:
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.