Hack Alert! You could do the evaluation of the PHP yourself with a bit of hackery using preg_replace_callback
to search and replace the PHP blocks.
function evalCallback($matches)
{
// [0] = <?php return returnOrEcho("hi1");?>
// [1] = <?php
// [2] = return returnOrEcho("hi1");
// [3] = ?>
return eval($matches[2]);
}
function evalPhp($file)
{
// Load contents
$contents = file_get_contents($file);
// Add returns
$content_with_returns = str_replace(
"returnOrEcho"
,"return returnOrEcho"
,$contents);
// eval
$modified_content = preg_replace_callback(
array("|(\<\?php)(.*)(\?\>)|"
,"evalCallback"
,$content_with_returns);
return $modified_content;
}
You would have to modify the PHP file you are including to use a returnOrEcho
function so that it can be overloaded for this case and the normal case. In this case you want to return
so that it will be picked up by the eval
in the way you want, but the normal case is to echo
without a return.
So for this case you would define:
function returnOrEcho($str)
{
return $str;
}
and for the normal case you would define:
function returnOrEcho($str)
{
echo $str;
}
In your included PHP file (or view file) you would have something like this:
<?php returnOrEcho("hi1");?>
<?php returnOrEcho("hi3"."oo");?>
<?php returnOrEcho(6*7);?>
I couldn't get preg_replace_callback
inline callback working so I used a separate function, but there is an example of how to do it: preg_replace_callback() - Calback inside current object instance.