How to get the output HTML of a php file in php 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
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

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

Post 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...
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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;

?>
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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;
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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...
Post Reply