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

Related Questions

I'm getting some objects from an external library and I need to store those
I have a function from an external source that returns an array of 2
I have an external DLL whose source code is C#. From the documentation for
I use Eclipse with external projects - i.e. projects created from existing source. Poking
I am ftp'ing files from an external server, then deleting them on the external
I need to call an external dll from c#. This is the header definition:
I've received data from an external source, which is in a summarised format. I
Here is my basic use case: After Labview receives a trigger from external hardware
I am writing a C# program which captures signals from a external device, and
I've got a list of People that are returned from an external app and

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.