21

the output is like below restored in a output.txt file:

array (
  'IMType' => '1',
  'Email' => '[email protected]',
  'SignupName' => 'test11',
  'Password' => '11111',
  'Encrypted' => '',
  'Confirm' => '11111',
  'OldPassword' => '',
  'Name' => 'test',
  'SignupProvinceText' => 'province',
  'SignupCity' => 'cityname',
  'Street' => 'street x.y',
  'SignupIndustry' => 'IT',
  'SignupCompany' => 'jobirn',
  'SignupJt' => 'engineer',
  'CellPhoneNum' => '',
  'linked_in' => '',
)

it's in fact output of var_export(my_variable,true),but how to read it into a variable again?

5 Answers 5

27

like this:

$dumpStr = var_export($var,true);
eval('$somevar = ' . $dumpStr . ';');

https://www.php.net/manual/en/function.eval.php

Sign up to request clarification or add additional context in comments.

8 Comments

Be very careful with eval, and only use it if you are 100% completely sure that the user can't inject any code.
agreed about the danger there. hence my comment to Zilupe's answer.
Also, it should be eval('$somevar = ' . $dumpStr); as + is only used for addition. (If I remember correctly.)
hahah, quite right. That's what I get for working on javascript all day
Sometimes you should add ; for evaluating variable. For example: eval( '$somevar = '. $dumpStr .';' );
|
20

Perhaps you want to serialize object and then unserialize? http://php.net/serialize

4 Comments

the data structure is restored in a file.can it be directly imported without serialization?
yes, this is probably what he wants. And is safer than using eval
@Shore what do you mean by "directly imported without serialization"? Serialization is the process of storing an object. Unserialization is the restoration of the stored object. You can restore objects serialized with anything you wish, but they should follow the format (consult PHP.net)
@Jonathan, it is NOT safer than eval. Quote from the doc: Warning Do not pass untrusted user input to unserialize(). Unserialization can result in code being loaded and executed due to object instantiation and autoloading, and a malicious user may be able to exploit this. Use a safe, standard data interchange format such as JSON (via json_decode() and json_encode()) if you need to pass serialized data to the user.
11

its very simple , you must save it as php file not txt and then

just include it inside a variable :)

like this:

    file_put_contents( '/some/file/data.php', 
                       '<?php return '.var_export( $data_array, true ).";\n" );

when you need to access it just do this :

$data = include '/some/file/data.php'

2 Comments

includes returns 1 on success and FALSE on failure. Using it that way will not result in the contents of data.php being assigned to $data.
@AlexanderGarden you are wrong. when you are returning result inside php file you can get value with include inside another variable like what i did for $data. look at 5# example of this link from php.net -> php.net/manual/en/function.include.php
2

This technique is good for data cache:

<?php
// reading data from DB or an API webservice etc.
$arrName = array();    
$arrName = call_procedure_here();

$strFileContent = '<?php' . PHP_EOL . '$arrName = ' . var_export($arrName,true) . ';' . PHP_EOL . '?>';
file_put_contents('cache_folder/arrayfilename.php', $strFileContent);

// later... from another process
include 'cache_folder/arrayfilename.php';
?>

Comments

1

I wrote a basic var_export parser, without eval: VarExportParser.php

Usage:

require_once('VarExportParser.php');
$parsed = VarExportParser::parse("array ('IMType' => '1', 'Email' => '[email protected]')");
print(json_encode($parsed).PHP_EOL);

Outputs:

{"IMType":"1","Email":"[email protected]"}

You can try js version of it here: text_transform.html (the var_export to json button)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.