Open In App

Split a comma delimited string into an array in PHP

Last Updated : 31 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given a long string separated with comma delimiter. The task is to split the given string with comma delimiter and store the result in an array.

Examples:

Input : geeks,for,geeks
Output : Array ( [0] => geeks [1] => for [2] => geeks )

Input : practice, code
Output : Array ( [0] => practice [1] => code )

Use explode() or preg_split() function to split the string in php with given delimiter.

PHP | explode() Function: The explode() function is an inbuilt function in PHP which is used to split a string in different strings. The explode() function splits a string based on a string delimiter, i.e. it splits the string wherever the delimiter character occurs. This functions returns an array containing the strings formed by splitting the original string.
Syntax:

array explode( separator, OriginalString, NoOfElements )

PHP | preg_split() Function: The preg_split() function operates exactly like split(), except that regular expressions are accepted as input parameters for pattern.

Syntax:

array preg_split( string $pattern, string 
$subject [, int $limit = -1 [, int $flags = 0 ]] )

Example:




<?php
  
// Use preg_split() function
$string = "123,456,78,000"
$str_arr = preg_split ("/\,/", $string); 
print_r($str_arr);
      
// use of explode
$string = "123,46,78,000";
$str_arr = explode (",", $string); 
print_r($str_arr);
?>


Output:

Array
(
    [0] => 123
    [1] => 456
    [2] => 78
    [3] => 000
)
Array
(
    [0] => 123
    [1] => 46
    [2] => 78
    [3] => 000
)

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


Similar Reads

How to create comma separated list from an array in PHP ?
The comma separated list can be created by using implode() function. The implode() is a builtin function in PHP and is used to join the elements of an array. implode() is an alias for PHP | join() function and works exactly same as that of join() function. If we have an array of elements, we can use the implode() function to join them all to form o
2 min read
How to Split a String into an Array in PHP ?
In PHP, splitting a string into an array involves breaking a string into smaller parts based on a delimiter or a pattern and storing those parts as elements of an array. This operation is useful for parsing text data, extracting substrings, or processing user input. PHP provides several functions to split a string into an array, including explode(
2 min read
How to convert a 2D array to a comma-separated values (CSV) string in JavaScript ?
Given a 2D array, we have to convert it to a comma-separated values (CSV) string using JS. Input: [ [ "a" , "b"] , [ "c" ,"d" ] ] Output: "a,b c,d" Input: [ [ "1", "2"] ["3", "4"] ["5", "6"] ] Output: "1,2 3,4 5,6" To achieve this, we must know some array prototype functions which will be helpful in this regard: Join function: The Array.prototype.j
3 min read
Convert comma separated string to array using JavaScript
In JavaScript, it's common to encounter comma-separated strings that need to be converted into arrays for further processing or manipulation. Whether you're parsing user input, handling data from an external source, or performing string operations, knowing how to convert a comma-separated string to an array is important. A comma-separated string ca
3 min read
How to Split Array into Specific Number of Chunks in PHP ?
This article will show you how to split an array into a specific number of chunks using PHP. There are basically two approaches to solve this problem, these are: Using array_chunk() functionUsing array_slice() functionApproach 1: Using array_chunk() functionThe array_chunk() function is used to split an array into parts or chunks of a given size de
2 min read
Create a comma separated list from an array in JavaScript
In this article, we will see how to convert an array into a comma-separated list using JavaScript. To do that, we are going to use a few of the most preferred methods. Methods to Create Comma Separated List from an Array: Table of Content Array join() methodArray toString() methodUsing Array literalsUsing Lodash _.join() methodUsing map() and join(
3 min read
How to split multiline string into an array of lines in JavaScript ?
Multiline string in JavaScript means a string having two or more lines. To split a multiline string into an array we need to use split() in our JavaScript code. JavaScript split(separator, limit) Method: The split() function is used to split the data depending upon the attributes we are passing in it. The separator attributes specify that from this
2 min read
Split a String into an Array of Words in JavaScript
JavaScript allows us to split the string into an array of words using string manipulation techniques, such as using regular expressions or the split method. This results in an array where each element represents a word from the original string. This can be achieved by using the following methods. Table of Content Using the split() methodUsing match
3 min read
Angular PrimeNG Form Chips Comma Separator Component
Angular PrimeNG is an open-source library that consists of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will see Angular PrimeNG Form Chips Comma Separator Component. The Chips Component in PrimeNG is used to take input of many values in
3 min read
How to use comma as list separator in AngularJS ?
In this article, we will use commas as a list separator in AngularJS applications. In AngularJS, we can use a list separator by simply placing the command between the items in the list. If we have an array of items that we need to display in the application, then we can use ng-repeat to iterate through this array and then display each item which is
4 min read