Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5957125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:21:22+00:00 2026-05-22T18:21:22+00:00

I’m writing a command line application in PHP that accepts a path to a

  • 0

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 { , try json_decode(), see if it fails.
  • If the first byte of the file is < or $, try include(), 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:21:23+00:00Added an answer on May 22, 2026 at 6:21 pm

    There isn’t much to optimize here. The magic bytes approach is already the way to go. But of course the actual deserialization functions can be avoided. It’s feasible to use a verification regex for each instead (which despite the meme are often faster than having PHP actually unpack a nested array).

    base64 is easy enough to probe for.

    json can be checked with a regex. Fastest way to check if a string is JSON in PHP? is the RFC version for securing it in JS. But it would be feasible to write a complete json (?R) match rule.

    serialize is a bit more difficult without a proper unpack function. But with some heuristics you can already assert that it’s a serialize blob.

    php array scripts can be probed a bit faster with token_get_all. Or if the format and data is constrained enough, again with a regex.

    The more important question here is, do you need reliability – or simplicity and speed?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.