Introduction
In PHP, splitting a string into smaller parts is a normal task that every developer has to perform in their coding journey. It means breaking down a string into an array of substrings based on a specified delimiter or pattern. For example, let's say you have a string containing a comma-separated list of fruits, like "apple,banana,orange,grape". By splitting this string using the comma as the delimiter, you can obtain an array of individual fruits: ["apple", "banana", "orange", "grape"]. Splitting strings is helpful in many situations, like extracting data from formatted input, parsing user-provided values, or manipulating text to extract specific information.

In this article, we will discuss different methods to split strings in PHP with examples to understand their implementation properly.
Examples
Let's discuss some of the examples of splitting strings in PHP using various functions and techniques.
Using explode() Function
The explode() function is one of the most commonly used functions for splitting strings in PHP. It takes two parameters: the delimiter & the string to be split. The delimiter is the character or string that separates the substrings within the original string.
For example:
$fruits = "apple,banana,orange,grape";
$fruitArray = explode(",", $fruits);
print_r($fruitArray);
Output:
Array
(
[0] => apple
[1] => banana
[2] => orange
[3] => grape
)
In this example, we have a string $fruits containing a comma-separated list of fruits. We use the explode() function, specifying the comma "," as the delimiter and the $fruits string as the second parameter. The function splits the string and returns an array $fruitArray containing the individual fruits.
Using preg_split() Function and Regular Expression
The preg_split() function is another powerful tool for splitting strings in PHP. It allows you to split a string based on a regular expression pattern. This is particularly useful when splitting a string using a more complex delimiter or pattern.
For example:
$text = "Hello, world! How are you?";
$words = preg_split("/[,!?]+/", $text);
print_r($words);
Output:
Array
(
[0] => Hello
[1] => world
[2] => How are you
)
In this example, we have a string $text containing a sentence with various punctuation marks. We use the preg_split() function & provide a regular expression pattern "/[,!?]+/" as the delimiter. The regular expression pattern "[,!?]+" matches one or more occurrences of commas, exclamation marks, or question marks. The function splits the string based on these delimiters & returns an array $words containing the individual words or phrases.
Using strtok() Function
The strtok() function is another way to split strings in PHP. It allows you to split a string into smaller strings (tokens) based on a specified delimiter. The strtok() function is useful when you need to iterate over the tokens one by one.
For example:
$string = "This is a sample string";
$token = strtok($string, " ");
while ($token !== false) {
echo $token . "\n";
$token = strtok(" ");
}
Output:
This
is
a
sample
string
In this example, we have a string $string containing a sentence. We use the strtok() function & specify the space character " " as the delimiter. The function returns the first token, which is the substring before the first occurrence of the delimiter. We then use a while loop to iterate over the remaining tokens. Inside the loop, we echo each token & call strtok() with only the delimiter to retrieve the next token. The loop continues until no more tokens are found (i.e., strtok() returns false).
Using sscanf() Function
The sscanf() function is typically used for parsing formatted strings, but it can also be used to split strings in PHP. It allows you to split a string based on a specified format.
For example:
$string = "Gaurav Singh 25 New York";
$data = sscanf($string, "%s %s %d %s");
print_r($data);
Output:
Array
(
[0] => Gaurav
[1] => Singh
[2] => 25
[3] => New
)
In this example, we have a string $string containing a person's information: name, age, & city. We use the sscanf() function & provide a format string "%s %s %d %s" that specifies the expected format of the input string. The format string indicates that we expect two strings (%s), an integer (%d), & another string (%s). The function splits the string based on the format & returns an array $data containing the individual elements.
Using substr() and strpos() Functions
You can also split a string by using a combination of the substr() and strpos() functions. The substr() function extracts a substring from a string, while the strpos() function finds the position of the first occurrence of a substring within a string.
For example:
$string = "Hello, how are you?";
$delimiter = ",";
$substrings = [];
$startPos = 0;
while (($pos = strpos($string, $delimiter, $startPos)) !== false) {
$substrings[] = substr($string, $startPos, $pos - $startPos);
$startPos = $pos + strlen($delimiter);
}
$substrings[] = substr($string, $startPos);
print_r($substrings);
Output:
Array
(
[0] => Hello
[1] => how are you?
)
In this example, we have a string $string containing a sentence with a comma as the delimiter. We initialize an empty array $substrings to store the split substrings and a variable $startPos to keep track of each substring's starting position.
We use a while loop to find the position of the delimiter using strpos(). If the delimiter is found, we extract the substring from $startPos to the position of the delimiter using substr() and add it to the $substrings array. We then update $startPos to the position after the delimiter.
After the loop, we extract the remaining substring from $startPos to the end of the string and add it to the $substrings array.
Finally, we print the resulting array $substrings containing the split substrings.
Using str_getcsv() Function
The str_getcsv() function is specifically designed to parse CSV (Comma-Separated Values) strings. It splits a string into an array based on the specified delimiter and enclosure characters.
For example:
$csvString = "Akash,Tyagi,25,New Delhi";
$data = str_getcsv($csvString);
print_r($data);
Output:
Array
(
[0] => Akash
[1] => Tyagi
[2] => 25
[3] => New Delhi
)
In this example, we have a CSV string $csvString containing comma-separated values. We use the str_getcsv() function to split the string into an array $data. By default, the function uses a comma as the delimiter and a double quote as the enclosure character. You can specify custom delimiter and enclosure characters as additional parameters to the function if needed.
Note: The str_getcsv() function is useful when you are working with CSV data, as it handles the splitting and considers the enclosure characters that may be used to wrap values containing the delimiter.