Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 24th, 2005, 07:25 PM
Daniel Loose
Guest
 
Posts: n/a
Default 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

  #2  
Old August 24th, 2005, 08:15 PM
Philip Ronan
Guest
 
Posts: n/a
Default Re: Read *parsed* PHP file into a variable?

"Daniel Loose" wrote:
[color=blue]
> 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... ?!![/color]

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/


  #3  
Old August 24th, 2005, 08:25 PM
Kimmo Laine
Guest
 
Posts: n/a
Default Re: Read *parsed* PHP file into a variable?

"Daniel Loose" <noreply@web.de> kirjoitti
viestissä:430cb793.9399600@news.cs.tu-berlin.de...[color=blue]
> 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... ?!![/color]


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 <eternal.erectionN0@5P4Mgmail.com>


  #4  
Old August 24th, 2005, 08:45 PM
Daniel Loose
Guest
 
Posts: n/a
Default Re: Read *parsed* PHP file into a variable?

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.
  #5  
Old August 24th, 2005, 09:05 PM
Janwillem Borleffs
Guest
 
Posts: n/a
Default Re: Read *parsed* PHP file into a variable?

Daniel Loose wrote:[color=blue]
> 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.
>[/color]

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


JW



  #6  
Old August 24th, 2005, 09:25 PM
Daniel Loose
Guest
 
Posts: n/a
Default Re: Read *parsed* PHP file into a variable?

[color=blue]
>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.
>[/color]

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.
  #7  
Old August 25th, 2005, 12:25 PM
R. Rajesh Jeba Anbiah
Guest
 
Posts: n/a
Default Re: Read *parsed* PHP file into a variable?

Daniel Loose wrote:[color=blue]
> 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).[/color]
<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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles