469,328 Members | 1,260 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

Read *parsed* PHP file into a variable?

Hello,

I have this strange including problem:

I want to read a piece of HTML, residing in some file, into a
variable, not echo it out. So far so easy - but now the HTML contains
a line of PHP. And I wish not to get the PHP code into my variable but
the parsed result, just like if the variable was a client ;-). I wish
to keep the nice ?> ...<? HTML area (with highlighting in my editor),
and therefore include etc. don't help because they would echo it out.
heredoc also doesnt help (if this may come up to your mind).

Is there anything I can do...?!! The problem sounds so simple... the
more for such a great tool like PHP ... but... ?!!

Thanx a lot,
Daniel

PS Please post also if you know for sure that there is NO solution to
my problem

Aug 24 '05 #1
6 15134
"Daniel Loose" wrote:
Hello,

I have this strange including problem:

I want to read a piece of HTML, residing in some file, into a
variable, not echo it out. So far so easy - but now the HTML contains
a line of PHP. And I wish not to get the PHP code into my variable but
the parsed result, just like if the variable was a client ;-). I wish
to keep the nice ?> ...<? HTML area (with highlighting in my editor),
and therefore include etc. don't help because they would echo it out.
heredoc also doesnt help (if this may come up to your mind).

Is there anything I can do...?!! The problem sounds so simple... the
more for such a great tool like PHP ... but... ?!!


Easy: $html = file_get_contents('http://www.example.com/foo.bar');

Take a look here: <http://php.net/file_get_contents>

--
phil [dot] ronan @ virgin [dot] net
http://vzone.virgin.net/phil.ronan/
Aug 24 '05 #2
"Daniel Loose" <no*****@web.de> kirjoitti
viestissä:43**************@news.cs.tu-berlin.de...
Hello,

I have this strange including problem:

I want to read a piece of HTML, residing in some file, into a
variable, not echo it out. So far so easy - but now the HTML contains
a line of PHP. And I wish not to get the PHP code into my variable but
the parsed result, just like if the variable was a client ;-). I wish
to keep the nice ?> ...<? HTML area (with highlighting in my editor),
and therefore include etc. don't help because they would echo it out.
heredoc also doesnt help (if this may come up to your mind).

Is there anything I can do...?!! The problem sounds so simple... the
more for such a great tool like PHP ... but... ?!!

Sounds like you could use buffering.
<?php

ob_start(); // Start output buffering.
// Instead of outputting anything, the output is now stored in a buffer.
include('yourfile.php');
// There, now the file has been included and php parsed.
// It isn't output, it goes into the buffer.
$yourfile = ob_get_clean();
// Now we read everything that has been buffered... And stops buffering.

?>

Result: the file you included has been parsed by the php engine and stored
inside $yourfil, and nothing was output. Sounds like goal achieved.

--
SETI @ Home - Donate your cpu's idle time to science.
Further reading at <http://setiweb.ssl.berkeley.edu/>
Kimmo Laine <et****************@5P4Mgmail.com>
Aug 24 '05 #3
Just discovered http://de.php.net/eval and on that url especially the
user contributed function eval_html3(). But can't get it to work.

I made a simple test page:
http://mueller.zems.tu-berlin.de/~loose/test/test.php
Perhaps you feel like helping to fix the function. (Or to tell me what
I have misunderstood.) I just renamed eval_html3 to eval_html and
replaced <?php by <?. It's now supposed to work but it doesnt. Here
the test files:

test.php:
------------
$s = eval_html(file_get_contents('.js.php', 0));
$t = eval_html(file_get_contents('.css.php', 0));

function eval_html($string) {
$string = '<? ?>'.$string.'<? ?>';
$string = str_replace( '?>', '', str_replace( array( '<?', '<?' ),
'', preg_replace_callback( "/\?>(.*?)(<\?|<\?)/", "my_eval", $string )
) );
return eval($string);
}
function my_eval($arr) {
return ('echo stripslashes("'.addslashes($arr[0]).'");');
}

..js.php and .css.php:
------------------------------
contain plain js/ css plus one line
<? $a = 6+7; echo ".testClass { font-size: ".$a."px; }\n\n"; ?>
or resp.
<? $a = 4+5; echo 'testVar = '.$a;; ?>
No errors when called directly.

test.php itself does nothing (blank screen), I only want to get away
the errors. You may also download this small test if you want to
figure it out on your own system (See Link)

Thanx a lot! , Daniel

PS Thx to the other poster, will check your idea out soon as possible.
Aug 24 '05 #4
Daniel Loose wrote:
Just discovered http://de.php.net/eval and on that url especially the
user contributed function eval_html3(). But can't get it to work.


Better have a look at Kimmo's suggestion, because that's the way to go...
JW

Aug 24 '05 #5
Sounds like you could use buffering.
<?php

ob_start(); // Start output buffering.
// Instead of outputting anything, the output is now stored in a buffer.
include('yourfile.php');
// There, now the file has been included and php parsed.
// It isn't output, it goes into the buffer.
$yourfile = ob_get_clean();
// Now we read everything that has been buffered... And stops buffering.


Pretty cool, thanx so much! Never heard of output buffering before.
Yeah...

Guess no need to fight with my other Reply anymore (concerning
eval_html()).

Enjoy, D.
Aug 24 '05 #6
Daniel Loose wrote:
Hello,

I have this strange including problem:

I want to read a piece of HTML, residing in some file, into a
variable, not echo it out. So far so easy - but now the HTML contains
a line of PHP. And I wish not to get the PHP code into my variable but
the parsed result, just like if the variable was a client ;-). I wish
to keep the nice ?> ...<? HTML area (with highlighting in my editor),
and therefore include etc. don't help because they would echo it out.
heredoc also doesnt help (if this may come up to your mind).

<snip>

Sounds like you're going to use/eval users' input?--if so, it's a
*serious* security issue. Use output buffering with runkit
<http://in.php.net/runkit>

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com

Aug 25 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by chuck amadi | last post: by
6 posts views Thread by Steve | last post: by
3 posts views Thread by Rakesh Sinha | last post: by
2 posts views Thread by ad | last post: by
2 posts views Thread by PJ6 | last post: by
6 posts views Thread by Samuel M. Smith | last post: by
1 post views Thread by jim.omalley | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.