tags:

views:

80

answers:

7

I have an comma seperated list of values held as a string:

blue, left-right-middle, panther.png

I then have three functions:

  1. Sets background colour

  2. Sets order of columns for layout

  3. Sets a profile image

At the moment I use a for each loop to explode the values into seperate strings, but how can I better control the results.

E.g

First result of array = Sets background colour
    Second result of array = Sets order of columns
    Third results of array = profile image

Can I write the results of the array to 3 seperate variables in anyway, so I can assign the variable to each function?

Like so:

    First result of array = $backgroundColour
    Second result of array = $orderColumns
    Third results of array = $profileImage

Any ideas how I might go about this?

+5  A: 

As of PHP5.3 You could use str_getcsv() to parse a CSV string into an array.
Also, have a look at list to assign variables as if they were an array.

list( $color, $order, $image ) = str_getcsv($csvString);

Prior to PHP5.3, you'd use explode instead of str_getcsv. See example by @poke below.

The advantage of str_getcsv over explode is that you can specify delimiter, enclosure and escape character to give you more control over the result.

str_getcsv is smart enough to trim whitespace automatically. The listed values would contain

string(4) "blue", string(17) "left-right-middle", string(11) "panther.png"`

However, added control does cost speed. explode is substantially (~6 to 8 times on my machine) faster for the given example string.

Gordon
But when you have the string exactly as formatted above (with `', '` as delimiter), `str_getcsv` won't work, because the delimiter is required to be only one character (and in this example, you also need neither enclosure nor escape, as the possible values of those string is quite restricted).
poke
@poke see update above
Gordon
Oh, I see, thanks for the update :)
poke
+4  A: 

Use list:

$line = 'blue, left-right-middle, panther.png';
list( $bkgColor, $columnOrder, $profileImage ) = explode( ', ', $line );

echo 'Background color: ' . $bkgColor . "<br />\n";
echo 'Column order: ' . $columnOrder. "<br />\n";
echo 'Profile image: ' . $profileImage . "<br />\n";
poke
+1  A: 

You could make use of explode and list:

$string = 'blue, left-right-middle, panther.png';
list($backgroundColour, $orderColumns, $profileImage) = explode(', ', $string);
RJD22
A: 
$array = explode(', ', 'blue, left-right-middle, panther.png');
list($color, $position, $image) = $array;
echo $color; // blue
echo $position; //left-right-middle
echo $image; //panther.png
jackbot
A: 

May you use the str_getcsv function of php.

See in manual: http://www.php.net/manual/en/function.str-getcsv.php

u2ix
A: 

Use this following code.

$string = 'blue, left-right-middle, panther.png';

list($color, $position, $image) = split (', ',$string);

echo 'Background color: ' . $color ."
\n";

echo 'Column order: ' . $postion. "
\n";

echo 'Profile image: ' . $image . "
\n"

pavun_cool
A: 

Just to be different, you could use sscanf

$string = 'blue, left-right-middle, panther.png';

sscanf( $string, '%s, %s, %s', $backgroundColour, $orderColumns, $profileImage );
meouw