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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
prov
Forum Newbie
Posts: 10
Joined: Thu Mar 25, 2004 9:13 pm
Location: Providence, RI
Contact:

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

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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');
?>
prov
Forum Newbie
Posts: 10
Joined: Thu Mar 25, 2004 9:13 pm
Location: Providence, RI
Contact:

Post by prov »

Excellent, redmonkey, that did the job. :D Thanks a lot.
Post Reply