Open In App

How to Split String by Delimiter/Separator in PHP?

Last Updated : 09 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Splitting a string by a delimiter in PHP is a common task that involves breaking a string into smaller components based on a specific character or pattern. This technique is essential for processing and manipulating strings in various applications, such as parsing CSV data or handling user inputs.

Examples:

Input: apple,orange,banana
Output:
    apple
    orange
    banana

There are seven approaches to split the string, these are:

Using explode() Function

The explode() function in PHP splits a string into an array based on a specified delimiter. It's ideal for simple, single-character delimiters, and allows you to quickly separate string components. The function returns an array of substrings split by the delimiter.

Example : In this example we plits a string into an array by commas and outputs each element of the array, one per line, using a loop.

PHP
<?php

$string = "apple,orange,banana";

// Split the string into an array
// using the comma as a delimiter
$fruitsArray = explode(',', $string);

// Output each element of the
// resulting array
foreach ($fruitsArray as $fruit) {
	echo $fruit . "\n";
}

?>

Output
apple
orange
banana

Using preg_split() Function and Regular Expression

The preg_split() function with a regular expression to split the string into an array, providing more flexibility for complex delimiter patterns.

Example: In this example we splits a string into an array using a regular expression to match commas, then outputs each element of the array in a loop.

PHP
<?php

$string = "apple,orange,banana";

// Split the string into an array using
// a regular expression to match commas
$fruitsArray = preg_split('/,/', $string);

// Output each element of the resulting array
foreach ($fruitsArray as $fruit) {
	echo $fruit . "\n";
}

?>

Output
apple
orange
banana

Using strtok() Function

The strtok() function in PHP tokenizes a string by splitting it using specified delimiters. It returns each token one at a time, making it useful for iterative processing of string segments in a loop.

Example : in this example we tokenizes a string by commas using strtok, then outputs each token in a loop until no more tokens are found.

PHP
<?php

$string = "apple,orange,banana";

// Use strtok to tokenize the
// string based on commas
$token = strtok($string, ',');

// Output each token until there
// are no more
while ($token !== false) {
	echo $token . "\n";
	$token = strtok(',');
}

?>

Output
apple
orange
banana

Using sscanf() Function

Utilizes the sscanf() function with a format specifier to extract substrings separated by commas into separate variables, providing a structured way to split the string.

Example: In this example we are using sscanf to split a comma-separated string into variables, then outputs each fruit on a new line.

PHP
<?php

$string = "apple,orange,banana";

// Use sscanf to scan for non-comma
// characters and split the string
sscanf($string, "%[^,],%[^,],%s", $fruit1, $fruit2, $fruit3);

// Output each fruit
echo $fruit1 . "\n";
echo $fruit2 . "\n";
echo $fruit3 . "\n";

?>

Output
apple
orange
banana

Using substr() and strpos() Functions

Manually splits the string by using substr() and strpos() to extract substrings before and after each comma, allowing for custom handling of each part of the split string.

Example : In this example we splits a comma-separated string using substr and strpos, assigns each part to a variable, and then outputs each fruit on a new line.

PHP
<?php

$string = "apple,orange,banana";

// Split the string using substr and strpos
$fruit1 = substr($string, 0, strpos($string, ','));
$restOfString = substr($string, strpos($string, ',') + 1);
$fruit2 = substr($restOfString, 0, strpos($restOfString, ','));
$fruit3 = substr($restOfString, strpos($restOfString, ',') + 1);

// Output each fruit
echo $fruit1 . "\n";
echo $fruit2 . "\n";
echo $fruit3 . "\n";

?>

Output
apple
orange
banana

Using str_getcsv() Function

The str_getcsv() function is another way to split a string by a delimiter. This function is typically used to parse CSV data but can also be used for splitting strings based on a specified delimiter.

Example: This method is particularly useful when working with data that follows a CSV format or when needing a robust solution for splitting strings by delimiters.

PHP
<?php
// Sample input
$inputString = "apple,orange,banana";

// Using str_getcsv() to split the string by a comma
$delimiter = ",";
$result = str_getcsv($inputString, $delimiter);

foreach ($result as $item) {
    echo $item . "\n";
}
?>

Output
apple
orange
banana

Next Article

Similar Reads