douzhao7014 2010-03-24 10:47
浏览 18
已采纳

使用php永久性地将变量写入php文件

I need to be able to permanently change variables in a php file using php.

I am creating a multilanguage site using codeigniter and using the language helper which stores the text in php files in variables in this format: $lang['title'] = "Stuff";

I've been able to access the plain text of the files using fopen() etc and I it seems that I could probably locate the areas I want to edit with with regular expressions and rewrite the file once I've made the changes but it seems a bit hacky.

Is there any easy way to edit these variables permanently using php?

Cheers

  • 写回答

6条回答 默认 最新

  • doushengyou2617 2010-03-24 10:55
    关注

    If it's just an array you're dealing with, you may want to consider var_export. It will print out or return the expression in a format that's valid PHP code.

    So if you had language_foo.php which contained a bunch of $lang['title'] = "Stuff"; lines, you could do something along the lines of:

    include('language_foo.php');
    $lang['title2'] = 'stuff2';
    $data = '$lang = ' . var_export($lang, true) . ';';
    file_put_contents('language_foo.php', '<?PHP ' . $data . ' ?>');
    

    Alternatively, if you won't want to hand-edit them in the future, you should consider storing the data in a different way (such as in a database, or serialize()'d, etc etc).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集