Outside file parsing to variable

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
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Outside file parsing to variable

Post by Bennettman »

Is there a method to parse a PHP file include-style, but store the output in a variable instead of printing it on the page?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Sure...

Code: Select all

<?php
<pre>
    // index.php
    // require or include...
    require 'foo.php';
    // printout the variable $foo, that is set in the required page...
    echo $foo;

?>

Code: Select all

<?php
    // foo.php
    // set the $foo variable for later use in index.php...
    $foo = 'bar';
?>
Last edited by JAM on Mon Aug 11, 2003 10:31 pm, edited 1 time in total.
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post by Bennettman »

Don't understand >_<

It doesn't really look like what I want, which is a variable containing whatever I'd get if I included a PHP file. Then again, I could be totally wrong and something like that DOES work >_>
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

[quote="Bennettman"]Don't understand >_<[quote]

Perhaps I missunderstood you...
Anyways, I edited the post and narrowed it down abit codewise.
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post by Bennettman »

Nope, that's definitely not what I was looking for - thanks anyway.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Is there any better explanation, examples perhaps, of what youre trying to achieve?
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post by Bennettman »

I'll try ;p

OK, here's the site. Typical index.php?page=news template-using-content-files deal. However, instead of using include "$page.php";, I want to organise it more by putting the content in a variable to be used later.

Right now I'm using $var = implode("",file("$page.php")); to put the whole file in the variable, which works fine for flat text files. Problem is, when I do this for files with PHP code, the code isn't parsed at all, and just goes straight into the variable, and ultimately into the HTML.

So what I'm trying for is something that'll parse the file in a way similar to include, but put the end result inside a variable in a way simlar to what I'm using, instead of putting it straight into the page.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Ah, oh, aha...

http://se.php.net/manual/en/function.eval.php, http://se.php.net/manual/en/ref.outcontrol.php and some combinatin of regular expressions i think would be an approach.

The manuals on eval has some interesting usernotes about something similiar to this...
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Output buffering:

Code: Select all

<?php
ob_start();
include('.. the file ..');
$string = ob_get_contents();
ob_end_clean();
?>
Last edited by McGruff on Wed Aug 10, 2005 10:46 pm, edited 1 time in total.
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post by Bennettman »

Thankyouthankyouthankyou! That was exactly what I was looking for! ^_^
Post Reply