使用php将变量永久写入php文件 [英] Permanently write variables to a php file with php
问题描述
我需要能够使用php永久更改php文件中的变量。
I need to be able to permanently change variables in a php file using php.
我使用codeigniter创建一个多语言站点,并使用语言助手以PHP格式存储变量中的php文件:
$ lang ['title'] =Stuff;
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";
我已经能够使用fopen访问文件的纯文本()等等,我似乎可以找到要编辑的区域与正则表达式和重写文件一旦我做了更改,但它似乎有点黑客。
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.
有什么方法可以使用php永久编辑这些变量?
Is there any easy way to edit these variables permanently using php?
干杯
推荐答案
如果它只是一个你正在处理的数组,你可能需要考虑 var_export
。它会以有效PHP代码的格式打印或返回表达式。
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.
所以如果你的language_foo.php包含一堆 $ lang ['title'] =Stuff;
行,可以按照以下行做:
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 . ' ?>');
或者,如果您不想在将来手动编辑它们,数据以不同的方式(例如在数据库中,或 serialize()
'd等)。
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).
这篇关于使用php将变量永久写入php文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!