How to Split String by Delimiter/Separator in PHP?
Given a String, the task is to split the string by the delimiter. In this case, the delimiter is comma “,”.
Examples:
Input: apple,orange,banana
Output:
apple
orange
banana
There are seven approaches to split the string, these are:
Table of Content
- Split String by Delimiter using explode() Function
- Split String by Delimiter using preg_split() Function and Regular Expression
- Split String by Delimiter using strtok() Function
- Split String by Delimiter using sscanf() Function
- Split String by Delimiter using substr() and strpos() Functions
Split String by Delimiter using explode() Function
The explode() function splits a string into an array based on a specified delimiter (in this case, a comma).
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" ; } ?> |
apple orange banana
Split String by Delimiter 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.
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" ; } ?> |
apple orange banana
Split String by Delimiter using strtok() Function
The strtok() function to tokenize the string based on a specified delimiter (comma in this case) iteratively 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 ( ',' ); } ?> |
apple orange banana
Split String by Delimiter 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.
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" ; ?> |
apple orange banana
Split String by Delimiter 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.
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" ; ?> |
apple orange banana
Contact Us