1

I am using the following command to print an array of data to a file saved as .php so i can use include to grab the array later.

$toFile = '$Data = '. var_export($DataArray,true).'; ';
file_put_contents( $filename.'.php', "<?php ".$toFile." ?>");

It is printed to the file formatted to make it easy to read but it ends up taking up a lot more space on the disk due to spaces and newline and such. Is there an easy way to remove the formatting so it take up less space. I thought of using str_replace which would work for new lines but not spaces due to the data might have spacing in it.

<?php $Data = array (
  'Info' => 
  array (
    'value1' => 'text here',
    'value2' => 'text here',
    'value3' => '$2,500 to $9,999',
  ), ....

to something like this

<?php $Data = array('Info'=>array('value1'=>'text here','value2'=>'text here','value3'=>'$2,500 to $9,999'),...

Thanks

EDIT: Is there a preg_replace pattern i can use to remove unwanted spaces ONLY outside of quotes?

briandonor
  • 63
  • 3
  • 11
  • If running shell command is acceptable: `php -w file.php > file.php` – dev-null-dweller Oct 15 '13 at 16:32
  • I would strongly suggest using a serialisation data format like JSON instead of creating runnable PHP code. There are big security dangers with creating a config file in a format that is runnable code. – Spudley Oct 16 '13 at 08:49
  • Stripping newlines afterwards isn't that reliable. It's best to write a custom export function, manual example: http://php.net/manual/en/function.var-export.php#54440 – mario Aug 21 '15 at 16:54

2 Answers2

4

Sorry for necroing, but if the question still is open: I had the same "problem", and I don't think JSON is the answer in my case because a PHP-Array just parses much more efficiently than a JSON-object.

So I just wrote a little function that returns minified PHP-output and in my pretty oversimplified test case it saved about 50% space - tendency for larger arrays should be a significantly higher percentage

<?php
function min_var_export($input) {
    if(is_array($input)) {
        $buffer = [];
        foreach($input as $key => $value)
            $buffer[] = var_export($key, true)."=>".min_var_export($value);
        return "[".implode(",",$buffer)."]";
    } else
        return var_export($input, true);
}



$testdata=["test","value","sub"=>["another","array" => ["meep"]]];
$d1 = var_export($testdata, true);
$d2 = min_var_export($testdata);
echo "$d2\n===\n$d1\n\n";
printf("length old: %d\nlength new: %d\npercent saved: %5.2f\n", strlen($d1),strlen($d2), (1-(strlen($d2)/strlen($d1)))*100);
Franz Gleichmann
  • 3,420
  • 4
  • 20
  • 30
  • Only for completeness you should read this answer explaining why you need file locking without using `flock()` and why you could use `return` inside of the included file: http://stackoverflow.com/a/7927642/318765 – mgutt Feb 23 '17 at 00:29
1

json_encode()ing the data might be a better approach. That condenses it down really small and there's even a function json_decode() to convert it back to an array.

Alternatively, print_r() has a second parameter that allows you to return it's output as a string. From there you can condense it yourself.

With var_dump() you could possibly do it using output buffering. Start output buffer using ob_start() and then use var_dump() as normal. Use ob_get_clean() to get the output of var_dump() and from there you can start removing all unnecessary characters.

Jonathon
  • 15,873
  • 11
  • 73
  • 92
  • Alternatively, if the data's not intended for anything but PHP to use later, there's `serialize()` as well. – Marc B Oct 15 '13 at 16:29
  • json would have been a good idea if i thought about it before creating the script but i need to make sure it works for current files without going and converting all the old files to json and i believe i would have to do the same with serialize().and print_r() produces simliar formatted results. – briandonor Oct 15 '13 at 17:32
  • How about using output buffering with `var_dump()`. That would let you grab the output and start trying to condense it all down. See my updated answer. – Jonathon Oct 16 '13 at 08:19
  • 2
    Agreed with the json_encode()` suggestion. However please note that `var_dump()` and `print_r()` are **not** a solution here: These functions are not intended to produce output that will be used later on; they are for debugging purposes only. The output cannot be reliably parsed back. If you must do this, then the OP is already using `var_export()` which is intended for it. However, I'd definitely recommend `json_encode()` as a more robust solution. – Spudley Oct 16 '13 at 08:57