Let's say I have a file "English.txt
" containing these lines :
$_LANG["accountinfo"] = "Account Information";
$_LANG["accountstats"] = "Account Statistics";
Note : the file extension is .txt
and there is nothing I can do to change that. There is no opening PHP tag (<?php
) or anything, just those lines, period.
I need to extract and actually get the $_LANG
array declared from these lines. How do I do that? Simply include
ing the file echoes every line, so I do
ob_start();
include '/path/to/English.txt';
$str = ob_get_clean();
Now, if I call eval
on that string, I get an syntax error, unexpected $end
. Any ideas?
Thanks.
eval(file_get_contents('English.txt'));
however, be sure NOBODY can change English.txt, it could be dangerous!
First of all, note that you should use file_get_contents
instead of include
with output buffering. Since it contains no <?php
tag, there is no need to run it through the script processor.
The following works perfectly in my tests:
<?php
$contents = file_get_contents("English.txt");
eval($contents);
var_dump($_LANG);
As one of the comments said, if you do the above and still get an error, then your file does NOT contain exactly/only those lines. Make sure the file is actually syntax compliant.
As has been mentioned, you should really use eval
only as a last resort, and only if the file is as safe to execute as any code you write. In other words, it must not be editable by the outside world.
精彩评论