我自動生成一些代碼並使用var_export輸出可解析格式的數組。任何想法,我怎麼可能讓它縮進因此它輸出的其他部分相匹配使用var_dump數組輸出的縮進
protected function getCode(){
$rs = ' $this->add(';
$rs .= var_export($this->getArray(),true);
$rs .= ');'.PHP_EOL;
return $rs;
}
我得到的輸出是一樣
$this->add(array (
'name' => 'notes',
'attributes' =>
array (
'label' => 'Date',
'label_attributes' =>
array (
'class' => 'col-md-4 control-label',
),
),
));
我想它是用正確的空格
$this->add(array (
'name' => 'notes',
'attributes' =>
array (
'label' => 'Date',
'label_attributes' =>
array (
'class' => 'col-md-4 control-label',
),
),
));
感謝。這工作。我添加了幾個preg_replace來整理第一行和最後一行。 \t'$ rawString = preg_replace(「/^[] {2}/m」,'',$ rawString); $ rawString = preg_replace(「/^[$]/m」,'$',$ rawString); $ rawString = preg_replace(「/^[);] {3}/m」,'));',$ rawString);' – codebrent