I'm writing a command line application in PHP that accepts a path to a local input file as an argument. The input file will contain one of the following things:
- JSON encoded associative array
- A
serialized()
version of the associative array - A base 64 encoded version of the
serialized()
associative array - Base 64 encoded JSON encoded associative array
- A plain old PHP associative array
- Rubbish
In short, there are several dissimilar programs that I have no control over that will be writing to this file, in a uniform way that I can understand, once I actually figure out the format. Once I figure out how to ingest the data, I can just run with it.
What I'm considering is:
- If the first byte of the file is
{
, tryjson_decode()
, see if it fails. - If the first byte of the file is
<
or$
, tryinclude()
, see if it fails. - if the first three bytes of the file match a:[0-9], try
unserialize()
. - If not the first three, try
base64_decode()
, see if it fails. If not:- Check the first bytes of the decoded data, again.
- If all of that fails, it's rubbish.
That just seems quite expensive for quite a simple task. Could I be doing it in a better way? If so, how?