0

lets say I have an output of var_export:

return array (
  'DatabaseMySQL_settings' => 
  array (
    'absPath' => 'C:/Dolgok/WEBprogramozas/_common/plugins/_framework/core/DatabaseMySQL_settings.php',
    'namespace' => '',
    'implements' => 
    array (
    ),
    'extendsFrom' => 'Framework_setting',
    'type' => 'C',
    'isAbstract' => false,
  ),

instead, I want to get this:

return array('DatabaseMySQL_settings'=>array('absPath'=>'DatabaseMySQL_settings.php','namespace'=>'','implements'=>array(),'extendsFrom'=>'Framework_setting','type'=>'C','isAbstract'=>false,),

So there is no newlines and spaces. How to achieve this?

John Smith
  • 6,129
  • 12
  • 68
  • 123
  • possible duplicate of [Remove formatting of var\_export() php](http://stackoverflow.com/q/19386252) -- Or you know, just write your own var_export implementation. – mario Aug 21 '15 at 16:51

2 Answers2

2

If the output you want will only be used for PHP later, I recommend using serialize() instead

taxicala
  • 21,408
  • 7
  • 37
  • 66
  • 1
    I know, but its an extra operation :) serialize / deserialize – John Smith Aug 21 '15 at 16:58
  • 1
    Indeed. But i've been reading about benchmarks and all of them point that serialize is faster. – taxicala Aug 21 '15 at 16:59
  • `var_export` is way sloppier than using `serialize`, because it invariable leads to `include`ing or `eval`ing generated code. It's best to avoid doing that wherever possible. Just use `json_encode` and `json_decode`. They are fast as shit and very safe to use. – Darth Egregious Aug 21 '15 at 17:19
  • 1
    The pair [`json_encode()`](http://php.net/manual/en/function.json-encode.php)/[`json_decode()`](http://php.net/manual/en/function.json-decode.php) is even faster than [`serialize()`](http://php.net/manual/en/function.serialize.php)/[`unserialize()`](http://php.net/manual/en/function.unserialize.php). Portability of the encoded representation of the data is a bonus. – axiac Aug 21 '15 at 17:20
  • 1
    That is true, but it depends on the later use of the output, with json encode/decode you cannot rehydrate an object of a class, you loose all reference and functionality. – taxicala Aug 21 '15 at 17:23
2

Check this function (var_export_min) in the PHP documentation comments section: http://php.net/manual/fr/function.var-export.php#54440

Maxence
  • 12,868
  • 5
  • 57
  • 69