Monday, 13 August 2018

How to Split a Comma Delimited/separated String into an Array in PHP?

Problem:

You have a string consisting of some comma separated sub-strings (see the following). With those sub-strings, you want to form an array.
<?php
$languages = “Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday”;
?>

Solution:

You can use explode() function to split the above string separating by comma and create an array. See the following example-
1
2
3
4
5
6
7
<pre>
<?php
$languages = “Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday”;
$lan_arr = explode(‘, ’, $languages);
print_r($lan_arr);
?>
</pre>
Output:
How it works:The explode() function split the string(second parameter – $language) into smaller pieces separated by the separator defined as the first parameter(, ).

0 comments:

Post a Comment