- slide 1 of 2
Why and Where is PHP String Split Used?
When data is stored in a database or when a MS Access file is exported, an option to export the data in CSV is given to the user. The data is a set of comma separated values. For instance the data could be exported as follows:
In cases like these, developers would split the string so that parts of the string can be accessed individually. Using PHP string split, a string can be stored in an array using simple functions provided by PHP and they can be operated upon. Other common cases where strings are split are date fields, so that the date, month and the year can be stored in an array, formatted as desired and displayed or manipulated for other operations.
- slide 2 of 2
PHP String Split: How to Use it?
Splitting strings in PHP can be done in two ways by using either the split() function or the explode() function. The PHP string split function (which is the split() function ) is deprecated, meaning, the php split() function will not be supported in the future versions of PHP. The current PHP version is PHP 5.3.0 and it is recommended that the PHP string split function be used by using the PHP explode() function which returns the same results.
Syntax of PHP explode():
PHP 4.0.1 + versions include an extra argument called limit which is of the type int.
explode(delimiter, string_to_be_split, limit)
The explode function takes three arguments:
1) delimiter - The delimiter should be a string. It is the string that is used as a parameter based on which strings are split and stored into arrays.
2) string_to_be_split - Enter the string to be split. It is a good practice to store this string in a variable and then use the variable in this part of the arguments list.
3) limit - An integer value that specifies the number of array elements into which the string has to be split based on the delimiter. This is an optional value and can be left out.
In this example, I have used a string that is delimited by commas. The string is stored in a variable. The print_r function is used to display the output on a web page. The first argument of the explode function takes a comma which is the delimiter in this string. The second argument takes the variable in which the string has been stored.
$numbers = "one,two,three,four,five,six,seven,eight,nine,ten";
When the PHP page is viewed using a web browser, the following output is displayed.
Array (  => one  => two  => three  => four  => five  => six  => seven  => eight  => nine  => ten )
Thus the strings can be split and stored in an array and accessed individually.
In this second example, I have used the explode function that takes three arguments. The only difference between the above example and the following example is that, you can restrict the number of elements of the array in which the string that has been split is stored.
$badstring = "This--is--a--bad--string";
print_r (explode("--",$badstring, 3));
Array (  => This  => is  => a--bad--string )
The code is pretty self explanatory. I have used 3 in the place where the limit argument has to be entered. So there are three elements present in the array. The first two contain the string that has been split, the last one contains the rest of the string along with the delimiters.