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.
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 ","