I'm a little stuck on trying to figure this one out, I think I understand the principles but just a little lost on the execution. Say we have:
$string = "Blue, Red, Yellow";
Which is a string read in from a DB (it includes the commas). Basically what I'm trying to do break these words apart and store them separately in an array (easier to work with). Will something like this work:
$string_parts = explode(",",$string);
Is it as easy as this, or is there a better way?
If you don't want to have spaces in the resulting array, add a space to the separator:
$string_parts = explode(", ", $string);
Note that this database design violates the First Normal Form.
If it works, then it's as easy as that.
I'd agree with
$string_parts = explode(",",$string);
Afterwards, I would go through each element and trim off whitespace.
for($i = 0; $i < sizeof($string_parts); $i++)
$string_parts[$i] = trim($string_parts[$i];
EDIT: Instead of going through a for-loop and trimming off whitespace, I'd suggest
$string = str_replace(" ", "", $string);
then exploding the string.