Page 1 of 1

Is there a way to echo a file that parses the PHP?

Posted: Tue Jul 20, 2004 11:23 am
by prov
I've been browsing the PHP documentation for a function that would do something like this:

Code: Select all

<?php
//foo.txt

$a = 1;
$b = 2;
$c = 3;

echo "a is " . $a . ", b is " . $b . ", and c is " . $c;

?>

Code: Select all

<?php
//bar.php

echo [some procedure that would:];

//outputs a is 1, b is 2, and c is 3;
Instead, of course, by using just plain echo, you have:

Code: Select all

<?php

echo join(file("foo.txt"), "");

/*outputs

<?php
//foo.txt

$a = 1;
$b = 2;
$c = 3;

echo "a is " . $a . ", b is " . $b . ", and c is " . $c;

?> 
*/
I need it outputted to a string, so include wouldn't work. Any help? I have this feeling there's going to be an easy solution.

Posted: Tue Jul 20, 2004 11:25 am
by feyd
I have not seen anything built in that would do it.. however, you could build one without a lot of pain.. although it may take a while..

Posted: Tue Jul 20, 2004 11:37 am
by redmonkey
Try....

Code: Select all

<?php
ob_start();
include('foo.txt');
$text = ob_get_contents();
ob_end_clean();
echo $text;
?>
You could wrap that up in a function if needs be....

Code: Select all

<?php
function get_output($file)
{
  ob_start();
  include($file);
  $text = ob_get_contents();
  ob_end_clean();
  return $text;
}

echo get_output('foo.txt');
?>

Posted: Tue Jul 20, 2004 11:50 am
by prov
Excellent, redmonkey, that did the job. :D Thanks a lot.