So let's say I have a String that looks like this: "apple, banana, orange"
. How do i split the text to get a Array that looks like this "apple", "banana", "orange"
.
Thanks in advance.
Asked
Active
Viewed 39 times
-8

Elias Konrad
- 108
- 1
- 9
-
2This is well-documented; have you tried anything so far? StackOverflow isn't a free code-writing service, and expects you to [**try to solve your own problem first**](http://meta.stackoverflow.com/questions/261592). Please update your question to show what you have already tried, showcasing the specific problem you are facing in a [**minimal, complete, and verifiable example**](http://stackoverflow.com/help/mcve). For further information, please see [**how to ask good questions**](http://stackoverflow.com/help/how-to-ask), and take the [**tour of the site**](http://stackoverflow.com/tour) :) – Obsidian Age Oct 05 '17 at 19:20
-
1PHP provides the [`explode()`](http://php.net/manual/en/function.explode.php) function exactly for this purpose. And the [`trim()`](http://php.net/manual/en/function.trim.php) function to remove the unwanted whitespace characters from the beginning and end of a string. – axiac Oct 05 '17 at 19:22
-
1Voted to close this because it's unlikely this question will be helpfull to anyone in the future. – icecub Oct 05 '17 at 19:32
1 Answers
1
Use the explode PHP function :)
<?php
$str = "apple, banana, orange";
print_r (explode(" ",$str));
?>

Andrew1996
- 426
- 1
- 3
- 15
-
2Yes, the space will work but exploding on the comma is probably what the OP meant. – waterloomatt Oct 05 '17 at 19:24
-
-
$a = explode("," ,"apple, banana, orange"); array_walk($result, function(&$element) { $element = '"'.$element.'"'; }, $a); print_r($result); – dmitri Oct 05 '17 at 19:34