开发者

import all data variables from a php file without rendering it

开发者 https://www.devze.com 2022-12-16 18:26 出处:网络
i want to import all the data var开发者_StackOverflowiables from a php file. but if i write: include \'user.php\';

i want to import all the data var开发者_StackOverflowiables from a php file. but if i write: include 'user.php';

it loads that php file instead of the current file. any solutions?


ob_start();
include 'user.php';
ob_end_clean();

// use any variable defined in user.php


If you are fine with executing it but want to suppress output you can use output_buffering, see ob_start() and related functions.

But in general this sounds like really bad design and should be refactored/cleaned-up instead.


You mean the PHP file gets executed?

There's no way around that, I'm afraid. If you only want to fetch variable values from the file, you'll either have to remove the unwanted commands from the file, or parse the data values out of it.


I'm not exactly sure what you are asking, but here are your options.

You could write a PHP parser with PHP using the Tokenizer extension, but that would be crazy.

If you just want to match the variable names you could use the following regular expression together with preg_match_all() and file_get_contents():

\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*

Another option is to use the output buffering functions.


I would take the "execution" part(as you call it), or nonprinting part, of the script you want to reuse(let's call it "display2") and put it into a separate php file("scriptfile"). Then, you can reference that second script from both "display2" and your new page.
Comment if you need clarification.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号