0

I try to store different languages in .ini files.

language_en.ini :

HELLO_MSG = "Hello $user ! How are you ?"

php file

$user = "Mike";
$ini_array = parse_ini_file("language_en.ini");
echo $ini_array['HELLO_MSG'];

Output

Hello Mike ! How are you ?

Is it possible to use the $user variable that i set in the ini file or do get just an array with strings if i read in the .ini file with the "parse_ini_file" method.

1 Answer 1

3

You should not be relying on variable interpolation for this anyway. You do not define a variable $user in this file, how can you expect it to be interpolated at any point? Will you define all possible variables before parsing the ini file? That's a mess!

The standard solution to this is sprintf:

$hello = "Hello %s! How are you?";
$message = sprintf($hello, $user);
echo $message;

Further, it seems what you're trying to reinvent here is gettext for localisation. Read this to realise why you want an existing system like gettext, not an ini file.

Sign up to request clarification or add additional context in comments.

1 Comment

Ooh great, "sprintf" is the solution i was looking for !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.