0

I am auto generating some code and using var_export to output an array in a parseable format. Any ideas as to how I might get it to indent so it matches the rest of the output

protected function getCode(){
    $rs = '            $this->add(';
    $rs .= var_export($this->getArray(),true);
    $rs .= ');'.PHP_EOL;
    return $rs;
}

The output I get is like

          $this->add(array (
  'name' => 'notes',
  'attributes' => 
  array (
    'label' => 'Date',
    'label_attributes' => 
    array (
      'class' => 'col-md-4 control-label',
    ),
  ),
));

I would like it to be with correct white space

          $this->add(array (
                       'name' => 'notes',
                       'attributes' => 
                       array (
                         'label' => 'Date',
                         'label_attributes' => 
                         array (
                           'class' => 'col-md-4 control-label',
                         ),
                       ),
                     ));
codebrent
  • 43
  • 1
  • 3

3 Answers3

1

Using preg_replace this is quite easy:

$array = array(
    array(
        "id" => 1,
        "foo" => "bar",
    ),
    array(
        "id" => 2,
        "foo" => "baz",
    ),
);

$str = var_export($array, true);
$str = preg_replace("/^/m", "    ", $str);
echo "    Indent:\n";
echo $str;
ccKep
  • 5,786
  • 19
  • 31
  • Thanks. This worked. I added a couple of preg_replace to tidy up first and last lines. `$rawString = preg_replace("/^[ ]{2}/m", ' ', $rawString); $rawString = preg_replace("/^[$]/m", ' $', $rawString); $rawString = preg_replace("/^[);]{3}/m", ' ));', $rawString);` – codebrent Jan 11 '14 at 04:42
0

You can't define the whitespace that var_dump() or var_export() puts into the text.

A better option would be to write a function that prints out the details of the array in the format you would like. Or, you can change the way you are parsing the text after it is printed to parse what var_dump() or var_export() gives you.

Edit:

My bad, I thought of another way that you could do it. It would require using str_replace(). var_dump() spits out the variable using \n and " " for formatting, so you could do something along the lines of:

echo str_replace(" ","&nbsp&nbsp&nbsp&nbsp", $this->getArray())

You would have to determine what you want the " " to be replaced with to get your desired formatting, and of course this would be designed to work on an HTML page. It's not the easiest thing to do (or the prettiest) but it as least something that you could try out if you don't want to write out your own function to print the array to your liking.

0

It is hard, it will take you a lot of energy. I think you can use this way to get a better format:

function getCode(){
    $rs = '$data = ';
    $rs .= var_export($this->getArray() ,true) . ';' . PHP_EOL;
    $rs .= '$this->add($data);' . PHP_EOL;
    return $rs;
}
echo getCode($data);

The output will be:

$data = array (
  'name' => 'notes',
  'attributes' => 
  array (
    'label' => 'Date',
    'label_attributes' => 
    array (
      'class' => 'col-md-4 control-label',
    ),
  ),
);
$this->add($data);
srain
  • 8,944
  • 6
  • 30
  • 42