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

  • Home
  • SEARCH
  • 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 937293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:27:16+00:00 2026-05-15T21:27:16+00:00

From an external source I’m getting strings like array(1,2,3) but also a larger arrays

  • 0

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?

  • 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-15T21:27:17+00:00Added an answer on May 15, 2026 at 9:27 pm

    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.)

    function parseArray($code) {
        $allowedTokens = array(
            T_ARRAY                    => true,
            T_CONSTANT_ENCAPSED_STRING => true,
            T_LNUMBER                  => true,
            T_DNUMBER                  => true,
            T_DOUBLE_ARROW             => true,
            T_WHITESPACE               => true,
        );
        $allowedChars = array(
            '('                        => true,
            ')'                        => true,
            ','                        => true,
        );
    
        $tokens = token_get_all('<?php '.$code);
        array_shift($tokens); // remove opening php tag
    
        foreach ($tokens as $token) {
            // char token
            if (is_string($token)) {
                if (!isset($allowedChars[$token])) {
                    throw new Exception('Disallowed token \''.$token.'\' encountered.');
                }
                continue;
            }
    
            // array token
    
            // true, false and null are okay, too
            if ($token[0] == T_STRING && ($token[1] == 'true' || $token[1] == 'false' || $token[1] == 'null')) {
                continue;
            }
    
            if (!isset($allowedTokens[$token[0]])) {
                throw new Exception('Disallowed token \''.token_name($token[0]).'\' encountered.');
            }
        }
    
        // fetch error messages
        ob_start();
        if (false === eval('$returnArray = '.$code.';')) {
            throw new Exception('Array couldn\'t be eval()\'d: '.ob_get_clean());
        }
        else {
            ob_end_clean();
            return $returnArray;
        }
    }
    
    var_dump(parseArray('array("a", "b", "c", array("1", "2", array("A", "B")), array("3", "4"), "d")'));
    

    I think this is a good comprimise between security and convenience – no need to parse yourself.

    For example

    parseArray('exec("haha -i -thought -i -was -smart")');
    

    would throw exception:

    Disallowed token 'T_STRING' encountered.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 483k
  • Answers 483k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The days of programs modifying or creating global registry keys… May 16, 2026 at 7:12 am
  • Editorial Team
    Editorial Team added an answer It will be easier to use an existing PostScript printer… May 16, 2026 at 7:12 am
  • Editorial Team
    Editorial Team added an answer First thing, is to forget about HTTP sessions, as these… May 16, 2026 at 7:12 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.