The PHP Include function - question for hack
Posted: Thu 29. Jan 2004, 02:41
I am working on using the same capability of the php include funtction to create a hack that will allow me to insert php code into the system rather than including a seperate file.
I think the include for the seperate file is great, but it is a pain if I need to use small snippets of code in php to have to include an entire seperate file just to do so.
It would be nice if I could just use the power of <? date() ?> every now and then. I know there are replacement tags for the date, but only in certain formats. Sometimes, small php snippets can be worth the time.
So, here is what I am trying.
I noticed that the replacement tag uses this function
include_ext_php("$1");
What I am trying to do, is re-create this code to have a replacement tag that is {CODE: <? some code ?>} .
I tried to use the buffer ability like this, but am having an issue.
if( ! ( strpos($content["all"],'{PHP:')===false ) ) {
// includes external PHP script and returns the content
$content["all"] = preg_replace('/\{PHP:(.*?)\}/e', 'include_ext_php("$1");', $content["all"]);
}
function run_php_code($code) {
// just uses the code in the replacement tag area.
$php_content = "";
ob_start();
$code_to_run = $code;
$php_content = ob_get_contents();
ob_end_clean();
return $php_content;
}
I figured that the $code variable could just be reset inside the buffer. It isn't producing anything.
So my questions are:
what is the "$1" used for in the function reference?
How do I achieve this? Is it an issue that I need to change in the regular expression?
Your help is greatly appreciated, the system is wonderful by the way.
Thanks,
Sean
I think the include for the seperate file is great, but it is a pain if I need to use small snippets of code in php to have to include an entire seperate file just to do so.
It would be nice if I could just use the power of <? date() ?> every now and then. I know there are replacement tags for the date, but only in certain formats. Sometimes, small php snippets can be worth the time.
So, here is what I am trying.
I noticed that the replacement tag uses this function
include_ext_php("$1");
What I am trying to do, is re-create this code to have a replacement tag that is {CODE: <? some code ?>} .
I tried to use the buffer ability like this, but am having an issue.
if( ! ( strpos($content["all"],'{PHP:')===false ) ) {
// includes external PHP script and returns the content
$content["all"] = preg_replace('/\{PHP:(.*?)\}/e', 'include_ext_php("$1");', $content["all"]);
}
function run_php_code($code) {
// just uses the code in the replacement tag area.
$php_content = "";
ob_start();
$code_to_run = $code;
$php_content = ob_get_contents();
ob_end_clean();
return $php_content;
}
I figured that the $code variable could just be reset inside the buffer. It isn't producing anything.
So my questions are:
what is the "$1" used for in the function reference?
How do I achieve this? Is it an issue that I need to change in the regular expression?
Your help is greatly appreciated, the system is wonderful by the way.
Thanks,
Sean