Question

I'm developing own CMS and i want to implement functionality to dynamically include existing PHP script which are located on the server.

there is variable called $page_content which contains page contents including HTML and JS code, also it contains some text recognised by regex, recognised text is then processed and replaced with desired dynamically created data. I also would like to trigger including new scripts by using that regex mechanism but here is a problem because regex recognition is solved by function and it seems that if i do "include" or "require" inside of function included script is limited by function variable scope so i can't get behaviour i need.

What should i do to make things works as i want, i mean that i can make bigger use of these dynamically included scripts.

Thanks in advance MTH

Was it helpful?

Solution

It sounds like dangerous stuff you're doing. Have you considered the case where the HTML/JS (which is inserted by the user of your CMS, I assume) contains strings matching your regex?

As for the scoping question: The function compact() can pack the current scope variables into an array and extract() can set them again. But be very very very cautious when using these functions. You might unexpectedly overwrite other variables you actually need.

function test($vars) {
    extract($vars);
    # The array might have contained the key 'vars', in which case
    # your function argument is now overwritten.
}

OTHER TIPS

have a look at the php extract function, it allows you to load an array of variables into the scope you want

http://nz2.php.net/manual/en/function.extract.php

ob_start();
extract($my_variables_array);
include $phpfile;
$output = ob_get_clean();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top