Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a comma-delimited string into an array?

I need to split my string input into an array at the commas.

Is there a way to explode a comma-separated string into a flat, indexed array?

Input:

9,[email protected],8 

Output:

['9', 'admin@example', '8']   
like image 214
Kevin Avatar asked Jul 14 '09 14:07

Kevin


People also ask

How do you store comma separated values in an array?

Answer: Use the split() Method You can use the JavaScript split() method to split a string using a specific separator such as comma ( , ), space, etc. If separator is an empty string, the string is converted to an array of characters.

How do you split a comma separated string?

To split a string with comma, use the split() method in Java. str. split("[,]", 0);

How can I convert a comma separated string to an array in PHP?

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. Use explode() or preg_split() function to split the string in php with given delimiter.

How do you parse a comma-delimited string in Java?

In order to parse a comma-delimited String, you can just provide a "," as a delimiter and it will return an array of String containing individual values. The split() function internally uses Java's regular expression API (java. util. regex) to do its job.


1 Answers

Try explode:

$myString = "9,[email protected],8"; $myArray = explode(',', $myString); print_r($myArray); 

Output :

Array (     [0] => 9     [1] => [email protected]     [2] => 8 ) 
like image 119
Matthew Groves Avatar answered Sep 23 '22 15:09

Matthew Groves