I have a comma-separated string that I want to convert into an array, so I can loop through it.
For example, I have this string
var str = "January,February,March,April,May,June,July,August,September,October,November,December";
Now I want to split this by the comma, and then store it in an array.
Of course I can write a custom function to do this. Or even better import one of the many existing libraries already written and well tested to process comma separated values, such as jquery-csv. But is there something built in?
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.
var array = string.split(',');
MDN reference, mostly helpful for the possibly unexpected behavior of the limit
parameter. (Hint: "a,b,c".split(",", 2)
comes out to ["a", "b"]
, not ["a", "b,c"]
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With