Page 1 of 1

Using eval() isn't working

Posted: Tue Aug 23, 2005 6:15 pm
by Chris Corbyn
Hi,

Never used eval() before so maybe I'm doing something wrong here....

I'm trying to read the contents of a PHP script which by itself echo()'s content to the page. I need to somehow read the output from within another script and store it in a string. I don't want to have to use shell access or use local loopback with apache.

I figured that somehow eval() would be a tasty option here but it seems to reject anything I throw at it..

The code I want to eval() - in another file

Code: Select all

<?php
$foo = 'bar';
echo $foo;
?>
and...
The file I'm trying to do it from

Code: Select all

<?php
$output = eval(file_get_contents('path/to/the/file.php'));
$ret = 'Foo'.$output;
echo $ret;
?>
The error message wrote:Parse error: parse error, unexpected '<' in /home/d11wtq/public_html/shane2/sys/core/core.class.php(327) : eval()'d code on line 1
I've also tinkered around with output buffering with little luck. If anyone knows what I'm doing wrong or a way to do it better please say (I do need to get the output from the other file either way).

Cheers,

d11

Posted: Tue Aug 23, 2005 6:18 pm
by nielsene
Try taking off the php tags in the file to be included. I think eval assume PHP mode.

Posted: Tue Aug 23, 2005 6:41 pm
by feyd
should be able to effectively use output buffering to capture the output from the when included ;)

Posted: Tue Aug 23, 2005 6:49 pm
by Chris Corbyn
Thanks nielsene that works but I have lots and lots of files that were previously called with the include() function.... now I want to capture the output in a string and if eval() won't allow <?php ?> tags it's probably the wrong route :?

feyd, do you mean use ob_ and simply stick include() in there? I hadn't thought of that... I'll try it.. ;)

Posted: Tue Aug 23, 2005 6:53 pm
by Chris Corbyn
feyd, you're a genius!

If anyone's wondering...

Code: Select all

<?php
ob_start();
include('path/to/the/file.php');
$output = ob_get_contents();
ob_end_clean();
$ret = 'Foo'.$output;
echo $ret;
?>