String segmentation is implemented through The explode () function. The explode () function splits a string according to the specified rules. the returned value is an array. The syntax format is as follows:
Split string
String segmentation is implemented through The explode () function. The explode () function splits a string according to the specified rules. the returned value is an array.
The syntax format is as follows:
explode(separator,string,limit)
The function parameter list is as follows:
| Parameter |
Description |
| Separator |
Required parameter to specify the delimiter. |
| String |
Required parameter to specify the string to be split |
| Limit |
Optional parameter, specifying the number of returned array elements |
Note: If separator is a null string (""), explode () returns false. if the value of separator cannot be found in string, explode () the function returns an array containing a single string element.
If the limit parameter is set, the returned array contains up to limit elements, and the final element contains the rest of the string. if the limit parameter is negative, returns all elements except the last-limit element.
Use the explode () function to split strings. the code is as follows:
From the code above, we can see that when splitting the string $ str, we split the string with "@" as the split identifier and split it into four array elements. Finally, we use var_dump () elements in the function output array.
The running result is as follows:
Array (4 ){
[0] => string (9) "PHP manual"
[1] => string (10) "HTML Manual"
[2] => string (9) "CSS Manual"
[3] => string (10) "JAVA manual"
}
Note: By default, the index of the first element of the Array is 0. For more information about arrays, see PHP Chinese network Array.
In addition to the var_dump () function, the output array element can also be output using the echo statement. The difference between the two is that the var_dump () function outputs an array column, the echo statement outputs a single element in the array.Var_dump ($ array );Use the following code to replace the elements in the array.
Output result:
PHP manual HTML Manual CSS Manual JAVA manual
Note: The running results of the preceding two methods for splitting strings are slightly different in form.
Synthesize string
The implode () function can combine the array content into a new string.
The syntax format is as follows:
implode(separator,array)
The separator parameter is optional. Specifies the content to be placed between array elements. The default value is "" (null string ). The array parameter is required and must be combined into an array of strings.
Use the implode () function to connect the content in the array with the separator * and combine the content into a new string. the code of the instance is as follows:
Output result:
PHP Manual * HTML Manual * CSS Manual * JAVA manual
Note:
The implode () and explode () functions are two opposite functions. one is used to synthesize strings and the other is used to separate strings.
The preceding sections describe how to parse string functions in PHP. For more information, see other related articles in the first PHP community!