Re: include into var

From: Date: Wed, 18 Oct 2000 19:48:54 +0000
Subject: Re: include into var
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-20971@lists.php.net to get a copy of this message
Carlos Henrique writes: > Hi there! > > I need to read and parse a html file into a var. > I can't use file() or fgets() because it don't parse the PHP tags in this > file. > > So, I'm using this: > > $content = include "file.php"; > print $content; > > It works fine. > > But for this work the content in "file.php" needs to be something like: > > return "<table > border=1>...<b><?=$varone;?></b>...</table>"; > > How can I do the same thing without placing all the file into a > return() statement? > > Regards, > Carlos Two options: you'd either need to suck the code into a string and run eval() on it, or use the output buffering functions to trap output to a variable for the duration of the inclusion: something like this: <?php error_reporting(E_ALL); ob_start(); include('test.html'); $included_file = ob_get_contents(); /* Next line gets output buffer contents & clears buffer. */ ob_end_clean(); echo "Got this text from the included file:\n" . htmlspecialchars($included_file); ?> Good luck, Torben -- +----------------------------------------------------------------+ |Torben Wilson <torben@php.net> Netmill iTech| |http://www.coastnet.com/~torben http://www.netmill.fi| |Ph: 1 250 383-9735 torben@netmill.fi| +----------------------------------------------------------------+

« previous php.general (#20971) next »