0

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

2
  • Can you give a code sample of what you're trying to achieve? Commented Jun 21, 2009 at 11:35
  • It would be rather difficult. In short words the thing is i got one or more variables like $page_content holding contents of lets we say "article" and while i'm including in my code other scripts by hand all works as i want it to work. When i make include from incsidee function things stop owrking. I figured out that is because limitations casued by variable scope in function so now i'm looking way to cope with that problem becasue synamically including of scripts would be usefull for me Commented Jun 21, 2009 at 12:25

2 Answers 2

1

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.
}
Sign up to request clarification or add additional context in comments.

1 Comment

i'm aware of danger but I think that I got already everything figured out at level that will leave no risk. This part of feature would be only avaliable for admin users I got also to experiment and read a little about solution provided in this answer and check if is it really what i need
0

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

https://www.php.net/manual/en/function.extract.php

ob_start();
extract($my_variables_array);
include $phpfile;
$output = ob_get_clean();

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.