Permanently write variables to a php file with php

2019-04-05 23:50发布

问题:

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

回答1:

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).



回答2:

It looks way easier to store data somewhere else (for instance, a database) and write a simple script to generate the *.php files, with this comment on top:

#
# THIS FILE IS AUTOGENERATED - DO NOT EDIT
#


回答3:

I once faced a similar issue. I fixed it by simply adding a smarty template. The way I did it was as follows:

  1. Read the array from the file
  2. Add to the array
  3. Pass the array to smarty
  4. Loop over the array in smarty and generate the file using a template (this way you have total control, which might be missing in reg-ex)
  5. Replace the file

Let me know if this helps.



回答4:

Assuming that

  • You need the dictionary file in a human-readable and human-editable form (no serializing etc.)

  • The Dictionary array is an one-dimensional, associative array:

I would

  1. Include() the dictionary file inside a function

  2. Do all necessary operations on the $lang array (add words, remove words, change words)

  3. Write the $lang array back into the file using a simple loop:

    foreach ($lang as $key => $value)
     fwrite ($file, "\$lang['$key'] = '$value';\n";
    

this is an extremely limited approach, of course. I would be really interested to see whether there is a genuine "PHP source code parser, changer and writer" around. This should be possible to do using the tokenizer functions.



回答5:

If it also is about a truly multilingual site, you might enjoy looking into the gettext extension of PHP. It falls back to a library that has been in use for localizing stuff for many years, and where tools to keep up with the translation files have been around for almost quite as long. This makes supporting all the languages in later revisions of the product more fun, too.

In other news, I would not use an array but rather appropriate definitions, so that you have a file

switch ($lang) {
case 'de':
  define('HELLO','Hallo.'); 
  define('BYE','Auf wiedersehen.'); 
break;
case 'fr':
   define('HELLO','Bonjour'); 
   define('BYE','Au revoir.'); 
   break;
case 'en':
default:
   define ('HELLO','Hello.'); 
   define ('BYE','Bye.'); 
}

And I'd also auto-generate that from a database, if maintenance becomes a hassle.



回答6:

Pear Config will let you read and write PHP files containing settings using its 'PHPArray' container. I have found that the generated PHP is more readable than that from var_export()