In php, two common functions are used to split strings: chunk_split and explode. There is also a str_split function. here is the example. Define and use the chunk_split () function
In php, two common functions are used to split strings: chunk_split and explode. There is also a str_split function. here is the example.
Definition and usage
The chunk_split () function splits a string into a series of smaller parts.
Syntax:Chunk_split (string, length, end)
Parameters:String is required, which specifies the string to be split.
Parameters:Length (optional) is a number that defines the length of a string block.
Parameters:End (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)
Parameters:The separator is required to specify where to split the string.
Parameters:String is required. the string to be split.
Parameters:Limit (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)
Parameters:String is required, which specifies the string to be split.
Parameters:Length (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