Outside file parsing to variable
Moderator: General Moderators
-
Bennettman
- Forum Contributor
- Posts: 130
- Joined: Sat Jun 15, 2002 3:58 pm
Outside file parsing to variable
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?
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
-
Bennettman
- Forum Contributor
- Posts: 130
- Joined: Sat Jun 15, 2002 3:58 pm
-
Bennettman
- Forum Contributor
- Posts: 130
- Joined: Sat Jun 15, 2002 3:58 pm
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.
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.
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...
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...
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