在Codeigniter中添加PHP行空间

时间:2014-07-06 06:12:21

标签: php codeigniter

我正在尝试编写文件,但只需要找出最佳方法,在某些代码“[‘default’][‘hostname’]” space . ‘=’ . space “‘localhost’”之间制作一些间隙/空格无法解决问题。

在重新加载页面时,它会生成$db['default']['hostname']='localhost';但需要间隙/空格$db['default']['hostname'] = 'localhost';

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

 public function index(){
  $output  = '<?php' . "\n";

  $output .= "\n";

  $output .= '// DB' . "\n";

  $output .= '$db' . "['default']['hostname']" space . '=' . space "'localhost'". ";" . "\n";

  $file = fopen(APPPATH . 'config/database-test.php', 'w');

  fwrite($file, $output);

  fclose($file);

  $this->load->view('welcome_message');
 }
}  

3 个答案:

答案 0 :(得分:0)

我想我已找到答案,让差距. " " .似乎可以解决问题。

$output .= '$db' . "['default']['hostname']" . " " . '=' . " " . "'localhost'". ";" . "\n";

答案 1 :(得分:0)

$output .= '$db' . "['default']['hostname']" space . '=' . space "'localhost'". ";" . "\n";

此行有语法错误,因为您在第一个space之前和第二个之后(无论空间是什么)都缺少concat运算符。

不要让事情复杂化,为什么不写这个并避免连续地狱:

$output .= '$db' . "['default']['hostname'] = 'localhost';\n";

答案 2 :(得分:0)

作为补充说明,我看到您正在编写配置文件以便稍后处理。

我发现输出缓冲和var_export非常好地完成了这项工作。

ob_start();

var_export($config);

$out = ob_get_clean();

然后

fwrite($f, '$config = '.$out.';'); etc...

http://us3.php.net/manual/en/function.ob-get-clean.php

http://us3.php.net/manual/en/function.var-export.php

基本上这会将数组转换为可解析的字符串,例如

array(
   'default'=>array(
        'hostname'=>'localhost',
        'user' => 'user', ///etc
    )
)

然后只需添加变量部分和结束分号

如果你计划做多个值,这将是一个更清洁的方法

相关问题