Page 1 of 1

How to get the output HTML of a php file in php variable...

Posted: Thu Feb 16, 2006 8:09 am
by raghavan20
I have a block of code which has php and HTML content in it. I want to execute this code and get the output HTML which I would be assigning to another PHP variable for use by render engine in templates.


I am saying something similar to executing a PHP file which has PHP and HTML content in it...but this is not a file but it is a set of code which has to be executed....

a simple example would be...

Code: Select all

<!-- Day Range -->
					<select name="day">
						<?php for ($i = 1; $i <= 31; $i++){?>
							<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
						<?php }	?>
					</select>
I want this block to be executed and I want the HTML output...hope you get what I am trying to tell you guys...

Posted: Thu Feb 16, 2006 9:07 am
by patrikG

Posted: Thu Feb 16, 2006 9:09 am
by Chris Corbyn
Simply:

Code: Select all

<?php

ob_start(); //Start output buffering (don't display things, store them in memory)
include('file.php');
$var = ob_get_clean(); //Clean out the buffer, get the contents

echo $var;

?>

Posted: Fri Feb 17, 2006 4:10 am
by raghavan20
d11wtq wrote:Simply:

Code: Select all

<?php

ob_start(); //Start output buffering (don't display things, store them in memory)
include('file.php');
$var = ob_get_clean(); //Clean out the buffer, get the contents

echo $var;

?>
guys, the problem is I cannot create a file, I will have a string variable which will hold all the HTML code combined with PHP code....the PHP function should be capable enough to parse PHP tokens with PHP tags and combine with existing HTML and finally should yield a complete HTML....

ex:

Code: Select all

$str = <<<EOT
hello all <?php echo "!!!"; ?>
<?php for ($i = 0; $i < 10; $i++){ ?>
hi guys!!!
<?php }?>
EOT;

Posted: Fri Feb 17, 2006 4:15 am
by patrikG

Posted: Fri Feb 17, 2006 10:54 am
by raghavan20
all these times, I was wondering what these functions are for but they have helped to find better solution for a problem...thanks to both of you...