-1

i havbe a language file in my framework , which is basically an array

eng/language.php

<?php
return [

    'HomeSearchtTitle'=>' search what you want ! ' ,
    'wellcom'=>' Wellcom !' ,
    'ResultSuccess'=>' here is the result ' ,
];
?>

i want to make the languages dynamic so the user can add new languages also be able to update this language.php file directly by php

so need to fetch this array inside the language.php viia file_get_contents and turn the string result to actual php array so i can present it to the user .... something like

$language = eval(str_replace(['<?php' , '?>' , 'return'  ] , '' , file_get_contents("$dir_path/language.php")));

this always returns null

any idea how i should do that ?

hretic
  • 999
  • 9
  • 36
  • 78
  • Think [this](https://stackoverflow.com/a/851764/1213708) sounds more like what you need. – Nigel Ren Dec 12 '20 at 17:54
  • Please keep [these simple rules](https://meta.stackoverflow.com/a/291370/1783163) in your posts. It is very hard to understand them. Your bounties won't help too much, you could get the same effect by following these rules. – peterh Feb 10 '21 at 18:26

1 Answers1

1

From the PHP manual:

It is possible to execute a return statement inside an included file in order to terminate processing in that file and return to the script which called it. Also, it's possible to return values from included files. You can take the value of the include call as you would for a normal function.

This means that you can simply do the following:

$languageArray = include "$dir_path/language.php";

To write back the modified array use var_export:

...
$languageArray['new_string'] = 'New String!';
file_put_contents("$dir_path/language.php", '<?php return ' . var_export($languageArray, true) . '; ?>');
Zoli Szabó
  • 4,366
  • 1
  • 13
  • 19