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!

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 trial


Brandon Kidd
Brandon Kidd
18,141 Points

Filtering 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.


Feel free to share, comment, expand on that solution if it is somthing you're looking for.

2 Answers

You 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;

Brandon Kidd
Brandon Kidd
18,141 Points

I'll give it a try and let you know if I have any questions (or if it works).

Thanks :)

Brandon Kidd
Brandon Kidd
18,141 Points

That worked :)

I'll post a writeup on my blog later with a link for all.

In the echo statement put "< li >" before $v and "< /li >" after. I don't know why is missing in my answer