Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBrandon Kidd
18,141 PointsFiltering Comma Separated Data
I need a little direction on where to begin.
I am working with simple text data that is separated by commas.
For example: item1, item2, item3 and so on.
My goal is to take this data and wrap each item in a list.
If anyone can point me in the correct direction, that would be helpful.
EDIT: Here is the code I was working on that sparked this question.This was to help in the construction of a Bootstrap pricing table using Advanced Custom Fields for WordPress.
https://gist.github.com/SmashBrando/5a629f329d64b46c9088
Feel free to share, comment, expand on that solution if it is somthing you're looking for.
2 Answers
George Cristian Manea
30,787 PointsYou should use the "explode" function like this: $text = "a,b,c,v"; $x= explode(",",$text); this will return an array // The first argument is the delimiter, in your case a comma; and the second is the string you want to divide. foreach ($x as $k=>$v){ echo "<li>".$v."</li>"; }
And if you want to put your string back together you can use implode: $comma_separated = implode(" ", $x);
//The first argument is the "glue" or a delimiter and the second argument is the array you what, in this case the array you got from explode;
George Cristian Manea
30,787 PointsIn the echo statement put "< li >" before $v and "< /li >" after. I don't know why is missing in my answer
Brandon Kidd
18,141 PointsBrandon Kidd
18,141 PointsI'll give it a try and let you know if I have any questions (or if it works).
Thanks :)
Brandon Kidd
18,141 PointsBrandon Kidd
18,141 PointsThat worked :)
I'll post a writeup on my blog later with a link for all.