I have an array of user inputs ($atts) as key=>value pairs. Some of the values could be written as an array expression, such as:
'setting' => 'array(50,25)'
In those cases, I would like to convert the array expression contained in that string into an actual array. So the output would be something like:
$atts = array(
'setting' => array(50,25),
'another' => 'not written as an array expression'
)
Written logically, the code would be:
For each key=>value pair in the array $atts…
if the value is a string formatted as an array expression…
explode that value into an array.
Anybody know how I would write this in PHP?
Use the tokenizer:
The above will work only for literals. Passing a variable, a constant, an expression, a nested array or a malformed array declaration will return
null:You can also use the following to check if your string is an array expression or not:
You can always tweak it to your needs. Hope this helps.