AbydosGater Posted July 16, 2007 Share Posted July 16, 2007 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 Quote Link to comment Share on other sites More sharing options...
Wildbug Posted July 16, 2007 Share Posted July 16, 2007 $Var = file_get_contents("http://server/file"); Quote Link to comment Share on other sites More sharing options...
fanfavorite Posted July 16, 2007 Share Posted July 16, 2007 Should be something like this: $file = fopen ($filename, "r"); $htmlcontent = fread($file); Quote Link to comment Share on other sites More sharing options...
fanfavorite Posted July 16, 2007 Share Posted July 16, 2007 Sorry yeah, use file_get_contents. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 16, 2007 Share Posted July 16, 2007 Output buffering perhaps? ob_start(); //code $var = ob_get_contents(); Quote Link to comment Share on other sites More sharing options...
AbydosGater Posted July 16, 2007 Author Share Posted July 16, 2007 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 Quote Link to comment Share on other sites More sharing options...
AbydosGater Posted July 16, 2007 Author Share Posted July 16, 2007 Hmm No sorry just noticed.. file_get_contents() is just like using fgets() its pulling the actual code for the page.. I need to just get the HTML output ... like including the file.. Is it possible? Quote Link to comment Share on other sites More sharing options...
Barand Posted July 16, 2007 Share Posted July 16, 2007 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); ?> Quote Link to comment Share on other sites More sharing options...
fanfavorite Posted July 16, 2007 Share Posted July 16, 2007 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. Quote Link to comment Share on other sites More sharing options...
AbydosGater Posted July 16, 2007 Author Share Posted July 16, 2007 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 16, 2007 Share Posted July 16, 2007 ~fanFavourite Have you got some working code to do that? Quote Link to comment Share on other sites More sharing options...
Wildbug Posted July 16, 2007 Share Posted July 16, 2007 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. Quote Link to comment Share on other sites More sharing options...
AbydosGater Posted July 16, 2007 Author Share Posted July 16, 2007 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? Quote Link to comment Share on other sites More sharing options...
AbydosGater Posted July 16, 2007 Author Share Posted July 16, 2007 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 Quote Link to comment Share on other sites More sharing options...
rlindauer Posted July 17, 2007 Share Posted July 17, 2007 As was mentioned earlier, output buffering would do this. Quote Link to comment Share on other sites More sharing options...
AbydosGater Posted July 17, 2007 Author Share Posted July 17, 2007 Yep. Thanks have it working now! Andy Quote Link to comment Share on other sites More sharing options...
Wildbug Posted July 17, 2007 Share Posted July 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.