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" ; ?> |
Output
apple orange banana
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
Contact Us