1

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

5
  • getting unexpected end like that probably means there's an unterminated string in the file. Commented Aug 3, 2011 at 20:25
  • I think for each line parsing two words between quotes (for eg, "accountinfo", "Account Information") and storing in an array is another solution that you can use. You cannot include file that does not indicate it is a php file. Commented Aug 3, 2011 at 20:25
  • It seems to work fine for me... at least with these two lines. Commented Aug 3, 2011 at 20:26
  • @Marc B. no, this file gets included in some protected code just fine, but I need to include another file just as this one somewhere else and I don't know how the protected code does it.... Commented Aug 3, 2011 at 20:27
  • @user482: you can include any kind of file you want. Until the PHP interpreter sees a <?php string go by, included files are treated as plain text, regardless of file extension. Commented Aug 3, 2011 at 20:32

2 Answers 2

1
eval(file_get_contents('English.txt'));

however, be sure NOBODY can change English.txt, it could be dangerous!

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

1 Comment

that file is the language file, and trust me, I sure am NOT the author of this atrocity! I just have to use it... anyhow. I'm not sure why this works and not with output buffering but, thanks!
1

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.

Comments

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.