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 Link to comment https://forums.phpfreaks.com/topic/60277-solved-load-the-html-output-of-a-file-into-a-variable/ Share on other sites More sharing options...
Wildbug Posted July 16, 2007 Share Posted July 16, 2007 $Var = file_get_contents("http://server/file"); Link to comment https://forums.phpfreaks.com/topic/60277-solved-load-the-html-output-of-a-file-into-a-variable/#findComment-299816 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); Link to comment https://forums.phpfreaks.com/topic/60277-solved-load-the-html-output-of-a-file-into-a-variable/#findComment-299817 Share on other sites More sharing options...
fanfavorite Posted July 16, 2007 Share Posted July 16, 2007 Sorry yeah, use file_get_contents. Link to comment https://forums.phpfreaks.com/topic/60277-solved-load-the-html-output-of-a-file-into-a-variable/#findComment-299819 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(); Link to comment https://forums.phpfreaks.com/topic/60277-solved-load-the-html-output-of-a-file-into-a-variable/#findComment-299821 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 Link to comment https://forums.phpfreaks.com/topic/60277-solved-load-the-html-output-of-a-file-into-a-variable/#findComment-299825 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? Link to comment https://forums.phpfreaks.com/topic/60277-solved-load-the-html-output-of-a-file-into-a-variable/#findComment-299834 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); ?> Link to comment https://forums.phpfreaks.com/topic/60277-solved-load-the-html-output-of-a-file-into-a-variable/#findComment-299845 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. Link to comment https://forums.phpfreaks.com/topic/60277-solved-load-the-html-output-of-a-file-into-a-variable/#findComment-299850 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. Link to comment https://forums.phpfreaks.com/topic/60277-solved-load-the-html-output-of-a-file-into-a-variable/#findComment-299854 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? Link to comment https://forums.phpfreaks.com/topic/60277-solved-load-the-html-output-of-a-file-into-a-variable/#findComment-299856 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. Link to comment https://forums.phpfreaks.com/topic/60277-solved-load-the-html-output-of-a-file-into-a-variable/#findComment-299922 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? Link to comment https://forums.phpfreaks.com/topic/60277-solved-load-the-html-output-of-a-file-into-a-variable/#findComment-299933 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 Link to comment https://forums.phpfreaks.com/topic/60277-solved-load-the-html-output-of-a-file-into-a-variable/#findComment-299936 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. Link to comment https://forums.phpfreaks.com/topic/60277-solved-load-the-html-output-of-a-file-into-a-variable/#findComment-299937 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 Link to comment https://forums.phpfreaks.com/topic/60277-solved-load-the-html-output-of-a-file-into-a-variable/#findComment-299967 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. Link to comment https://forums.phpfreaks.com/topic/60277-solved-load-the-html-output-of-a-file-into-a-variable/#findComment-299995 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.