How to read var_export output into a variable in PHP?

The output in the output.txt file is as follows:

array ( 'IMType' => '1', 'Email' => 'test@gmail.com', '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' => '', ) 

this is actually the output of var_export(my_variable,true) , but how to read it again in a variable?

+16
import php
Jun 01 '09 at 3:55
source share
5 answers

like this:

 $dumpStr = var_export($var,true); eval('$somevar = ' . $dumpStr.';'); 
+18
Jun 01 '09 at 3:57
source share

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

+18
Jun 01 '09 at 3:59
source share

This method is good for caching data:

 <?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'; ?> 
+2
Jul 29 '15 at 5:32
source share

it's very simple, you have to save it as a php file, not txt and then

just include it in the 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' 
0
Oct 30 '18 at 12:57
source share

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

Using:

 require_once('VarExportParser.php'); $parsed = VarExportParser::parse("array ('IMType' => '1', 'Email' => 'test@gmail.com')"); print(json_encode($parsed).PHP_EOL); 

Outputs:

 {"IMType":"1","Email":"test@gmail.com"} 

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

0
Jan 09 '19 at 15:54
source share



All Articles