1

I'm attempting to write a function that will take a string and create a drop down list.

Here is an example string:

Primary[Blue|0000FF,Red|FF0000,Yellow|FFFF00],Secondary[Green|00FF00,Orange|FF9900,Purple|663399],Brown|A52A2A,Silver|C0C0C0

That I want to turn into this:

<select>
<optgroup label="Primary">
<option value="0000FF">Blue</option>
<option value="FF0000">Red</option>
<option value="FFFF00">Yellow</option>
</optgroup>
<optgroup label="Secondary">
<option value="00FF00">Green</option>
<option value="FF9900">Orange</option>
<option value="663399">Purple</option>
</optgroup>
<option value="A52A2A">Brown</option>
<option value="C0C0C0">Silver</option>
</select>

I've been trying to convert the string to something like this:

[0] => Primary
[0][0] => Blue|0000FF
[0][1] => Red|FF0000
[0][2] => Yellow|FFFF00
[1] => Secondary
[1][0] => Green|00FF00
[1][1] => Orange|FF9900
[1][2] => Purple|663399
[2]
[2][0] => Brown|A52A2A
[3]
[3][0] => Silver|C0C0C0

I'm not sure how to split the initial string. I tried explode and preg_split, but neither seem to split where I need it to.

3
  • your string's pattern is not consistent. Commented Dec 5, 2012 at 20:02
  • 4
    Maybe just use json syntax? Commented Dec 5, 2012 at 20:03
  • explode( '],', $string ) will get an array containing [0] => "Primary[Blue|0000FF,Red|FF0000,Yellow|FFFF00", [1] => "Secondary[Green|00FF00,Orange|FF9900,Purple|663399", [2] => "Brown|A52A2A,Silver|C0C0C0" From there, you could explode each of those on "[", and the results from that can be exploded on "," Commented Dec 5, 2012 at 20:09

2 Answers 2

2

Not sure how you ended up with this string for i 'll advice you to consider json_decode and json_encode

For the current format you can use :

$string = 'Primary[Blue|0000FF,Red|FF0000,Yellow|FFFF00],Secondary[Green|00FF00,Orange|FF9900,Purple|663399],Brown|A52A2A,Silver|C0C0C0';
preg_match_all("/([a-z]+)(\[([a-z0-9|,]+)\])|([a-z0-9|,]+)$/i", $string, $m);

printf("<select>");
for($i = 0; $i < count($m[1]); $i ++) {
    if (! empty($m[1][$i])) {
        printf("\n\t<optgroup label=\"%s\">", $m[1][$i]);
        foreach ( array_filter(explode(",", $m[3][$i])) as $var ) {
            list($color, $hex) = explode("|", $var);
            printf("\n\t\t<option value=\"%s\">%s</option>", $hex, $color);
        }
        printf("\n\t</optgroup>");
    }
}
foreach ( array_filter(explode(",", $m[4][2])) as $var ) {
    list($color, $hex) = explode("|", $var);
    printf("\n\t<option value=\"%s\">%s</option>", $hex, $color);
}
printf("\n</select>");

Output

<select>
    <optgroup label="Primary">
        <option value="0000FF">Blue</option>
        <option value="FF0000">Red</option>
        <option value="FFFF00">Yellow</option>
    </optgroup>
    <optgroup label="Secondary">
        <option value="00FF00">Green</option>
        <option value="FF9900">Orange</option>
        <option value="663399">Purple</option>
    </optgroup>
    <option value="A52A2A">Brown</option>
    <option value="C0C0C0">Silver</option>
</select>
Sign up to request clarification or add additional context in comments.

Comments

0

You can try to split on this regex: (?![^\[]+\]),.

Then from there, see if the string contains [].

$array = preg_split('/(?![^\[]+\]),/', $string);

$return = array();

foreach($array as $val){
    if(preg_match('/(.*)\[(.*)\]/', $val, $match) === 1){
        $return[$match[1]] = explode(',', $match[2]);
    }
    else{
        $return[] = $val;
    }
}

DEMO: http://ideone.com/BM0ewX

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.