In the php tutorial, two common functions are used to split strings: chunk_split and explode. There is also a str_split function. See the instance below.
Definition and usage
The chunk_split () function Splits a string into a series of smaller parts.
Syntax
Chunk_split (string, length, end) parameter description
String is required. Specifies the string to be split.
Length is optional. A number that defines the length of a string block.
End is optional. String value, which defines the content placed after each string block.
*/
$ Data = "hello world! This is a world! "; // Define a string
$ New_string = chunk_split ($ data); // splits the string
Echo $ new_string; // output result
/*
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.
*/
$ Str = 'one | two | three | four '; // define a string
$ Result = explode ('|', $ str, 2); // cut the string
Print_r ($ result); // output result
$ Result = explode ('|', $ str,-1); // return the number with a negative number.
Print_r ($ result); // output result
/*
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.
*/
$ Str = "hello world"; // define a string
$ Result = str_split ($ str); // perform the conversion operation.
Print_r ($ result); // output the converted result
$ Result = str_split ($ str, 4); // each element is set to 4
Print_r ($ result); // output the converted result
?>