Code Snippets → PHP → Convert Comma Separated String into Array Convert Comma Separated String into Array Chris Coyier on Sep 13, 2009 (Updated on Oct 30, 2009) Easy way to turn a CSV file into a parseable array. <?php $str="foo,bar,baz,bat"; $arr=explode(",",$str); // print_r($arr); ?>
Is there a way to take a comma separated string like say:
Tags: Something, somethingelse, thirdsomethinginto
Perhaps by wrapping this code
Tags: Something, somethingelse, thirdsomethingin some kind of div? Is this possible?
Thank you very much,
Ok, but if i put the $arr in a cycle for example “for”…how can i kmow the lenght of that array?…the problem for me it’s that one…the cycle should print $arr,$arr,$arr,$arr and then stop, how can i tell it the it has to stop on number three?
Lezioni, you can always use a foreach loop rather than a for loop.
On the other had if you really want to now the lenth of an array
use the count statement.
Thanks a lot! Impressive simplicity!
That won’t work for a lot of cases. Consider:
Field 1, Field 1,”This is text, with a comma.”
That string will be broken into 4 pieces when it should only be 3.