Jump to content

[SOLVED] Load the HTML output of a file into a variable?


AbydosGater

Recommended Posts

Hi, Simple question. But i cant figure it out.. How can i load the html output of a file into a variable.. Kind of like include()ing or require()ing a file.. but not echoing the content.. but saving it to a variable..

 

Can it be done? I dont want to use fgets because they are PHP files.. and i want to get the HTML as if a user opened with a browser..

 

 

Andy

To be honest i dont actually understand the whole concept behind output buffering so i think i will stick to file_get_contents... Its working for me. But thank you Barand. I have something else that might require this.

 

Andy

Output buffering

 

::script.php::

<?php
echo "<strong>Hello</strong> World";
?>

 

::buffer.php::

<?php
ob_start();
include 'script.php';
$var = ob_get_contents();
ob_end_clean();

var_dump($var);
?>

Just a thought.  You may want to try php's strip_tags function.  You could assign the whole document as a variable then strip the html in another variable and then use str_replace to delete the strip_tags variable (which is all content other than html) out of the original variable leaving html left.  

Barrand: Ahh, Thats more understandable then what i have seen before.

Will get right on it.

 

Thank You

 

DanFavourite: I dont need to strip the html.. Its for my template class i need to keep the HTML.

 

Ill try using OB.. That or Eval.

Try this:

 

<pre><?php

$page = html_entity_decode(strip_tags(file_get_contents("http://yoururl/file")));

echo $page
?></pre>

 

I'm not sure which is more appropriate, html_entity_decode() or htmlspecialchars_decode().  I think html_entity_decode() is more thorough.

Im Not Trying to take out the HTML! I need the html and want the html. Its the PHP i dont want..

 

When you include a file into your application.. It runs it first, as if you opened said file in your browser.. and it returns the HTML for the browser to display..

 

I want to do this! But not display the code for the page.. But save it to a variable.

 

Unfortunately all methods i have tried..fgets().. file() and get_file_contents() have all returned the actuall PHP code for the page... before its been processed..

 

I need it after its been processed.. with all html tags..

 

But i cant find a way to do it

 

EDIT: Does eval(); echo or output the result after it has been called.. or can the result be saved to a variable to be outputted later?

To answer my last question.. I tried

<?php
$str = 'if (1 == 1) {echo "true";};';
$var = eval($str);
echo $var;
?>

 

And no it cant save the returned result to a variable it just outputs the result on being eval'd.

 

Does anyone know how i could get around this problem?

Please

Unfortunately all methods i have tried..fgets().. file() and get_file_contents() have all returned the actuall PHP code for the page... before its been processed..

 

Not if you use the URL wrapper, file_get_contents("http://www.yourserver.com/file.php").  Then it gets served through the webserver+PHP.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.