Executing php from another file

后端 未结 3 1342
逝去的感伤
逝去的感伤 2021-01-16 19:06

i have 1 template-file, which is included into main file.

some.template:

...

...

in mai

相关标签:
3条回答
  • 2021-01-16 19:12

    You could use include to read and process the file. If you need to post-process the result (I assume that might be the reason for you to read the template into a variable) try output buffering:

    ob_start();
    include 'some.template';
    $out = ob_get_clean();
    echo $out;
    

    If you need to modify the template before having PHP process it, you will have to resort to eval. But I would advise you to be very careful with this for obvious security reasons and maybe-not-so-obvious performance reasons. If you just need some variables to be replaced, you should consider using a template engine instead or simple means like preg_replace.

    0 讨论(0)
  • 2021-01-16 19:15

    Do not read the file into variable, instead do

     include 'some.template';
    
    0 讨论(0)
  • 2021-01-16 19:25

    Since you wish to edit the script before using it, the only way to do this practially is to use:

    eval($string);
    

    I'm sure I don't need to tell you not to do this with user input if you value security at all.

    0 讨论(0)
提交回复
热议问题