From an external source I’m getting strings like
array(1,2,3)
but also a larger arrays like
array("a", "b", "c", array("1", "2", array("A", "B")), array("3", "4"), "d")
I need them to be an actual array in php. I know I could use eval but since it are untrusted sources I’d rather not do that. I also have no control of the external sources.
Should I use some regular expressions for this (if so, what) or is there some other way?
Whilst writing a parser using the Tokenizer which turned out not as easy as I expected, I came up with another idea: Why not parse the array using
eval, but first validate that it contains nothing harmful?So, what the code does: It checks the tokens of the array against some allowed tokens and chars and then executes eval. I do hope I included all possible harmless tokens, if not, simply add them. (I intentionally didn’t include HEREDOC and NOWDOC, because I think they are unlikely to be used.)
I think this is a good comprimise between security and convenience – no need to parse yourself.
For example
would throw exception: