/    Sign up×
Community /Pin to ProfileBookmark

Interpreting a txt file that contains php vars

I have a good chunk of html and text in a separate fie that I am reading in to my script using file_get_contents. It also contains some php vars. This file is then output to the screen using a heredocs wrapper. The problem is that the heredocs is not translating the php vars into their values.

How does one accomplish this kind of thing?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 20.2021 — Hmm...the quick and dirty way that first pops to my mind:
[code=php]
eval("echo <<<EODn".file_get_contents($the_file)."nEOD;");
[/code]

...but I hate using eval() if I don't really have to. :|

I shall think more about it over lunch....
Copy linkTweet thisAlerts:
@ginerjmauthorAug 20.2021 — Forgot there was that function. Never had a use for it. I shall await further thoughts from you or others.
Copy linkTweet thisAlerts:
@ginerjmauthorAug 20.2021 — Actually I read up on the eval func and tried it and it works fine for me. Since this text file is wholly composed and maintained by me, outside of the web tree, I'm gonna stick with it unless there is truly a 'better' solution.
Copy linkTweet thisAlerts:
@NogDogAug 20.2021 — > @ginerjm#1635797 Since this text file is wholly composed and maintained by me, outside

Yeah, as long as you don't have to worry about something in the middle of it having text like...
<i>
</i>text text text
EOD;
exec('rm -fr .');
exit;

...you should be fine.
Copy linkTweet thisAlerts:
@ginerjmauthorAug 20.2021 — Thanks for the assistance!
Copy linkTweet thisAlerts:
@NogDogAug 20.2021 — No better ideas have come to mind, other than either:

  • - Altering those files to actually be included PHP file, where it's basically just one command: $text = "lots of $stuff here...";, then in the main script
    [code=php]
    include 'path/to/the/file';
    echo $text;
    [/code]

  • - Assuming it's a well-defined set of possible variables/replacements, use str_replace() or preg_replace() to explicitly set each such occurrence.
    [code=php]
    $replacements = [
    '$foo' => $foo,
    '$bar' => $bar
    ];
    $echo str_replace(array_keys($replacements), $replacements, file_get_contents('the_file.txt'));
    [/code]


  • I'm not jumping with joy for either of those, so... 🤷‍♂️
    Copy linkTweet thisAlerts:
    @ginerjmauthorAug 20.2021 — It's a structured HTML document that I want to add blocks of <Ii> tags to as they come to me. I figured it was easy to modify the text file rather than the php one. Since I am the only who can access it I'm not worried about hacking. And it's not that important anyway.

    The eval thingie will work just fine for me.

    Thx again.
    ×

    Success!

    Help @ginerjm spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.6,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...