Cenron Posted June 6, 2007 Share Posted June 6, 2007 Hey guys I got a quick question that I cant seem to figure out on my own. I want to achieve this ob_start(); include($file_path); $contents = ob_get_contents(); ob_end_clean(); but I want to load it from a variable instead of a file but I cant think of a way of doing it any idea guys? Quote Link to comment Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 ob_start(); echo $variable_name; $contents = ob_get_contents(); ob_end_clean(); .... I do not know why but that should work... Quote Link to comment Share on other sites More sharing options...
Cenron Posted June 6, 2007 Author Share Posted June 6, 2007 That did what I wanted it to but I got another problem from it...there is PHP code in the variable and with your method it dosent get parsed it just gets passed as normal text...include parsed it out and displayed only HTML..I am really trying hard from having to write another file with the variable info in it then include. I really do appreciate the help and that was a great idea. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted June 6, 2007 Share Posted June 6, 2007 If you have PHP code in a variable that you want to get executed, you need to use the eval() function. Use this function with caution. Ken Quote Link to comment Share on other sites More sharing options...
Caesar Posted June 6, 2007 Share Posted June 6, 2007 eval() Quote Link to comment Share on other sites More sharing options...
Cenron Posted June 6, 2007 Author Share Posted June 6, 2007 I might just not be doing this the right way Its still not parsing the PHP even though i run it through the eval. Here is the code i got so far. $temp_content = implode(file($this->html_code)); $temp_content = str_replace('\\', '\\\\', $temp_content); $temp_content = str_replace('\'', '\\\'', $temp_content); $stemp_content = explode("\n",$temp_content); for($i = 0; $i < sizeof($stemp_content); $i++) { $stemp_content[$i] = 'echo \'' . $stemp_content[$i] . '\' . "\\n";'; } $temp_content = implode("\n",$stemp_content); ob_start(); echo eval($temp_content); $contents = ob_get_contents(); ob_end_clean(); return $contents; Quote Link to comment Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 ob_start(); eval($temp_content); $contents = ob_get_contents(); ob_end_clean(); return $contents; I do not think you echo eval's. Quote Link to comment Share on other sites More sharing options...
Cenron Posted June 6, 2007 Author Share Posted June 6, 2007 Ya I got rid of the echo and it still doesn't eval...man this sucks lol Quote Link to comment Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 eval is a very touchy, and probably not the best function to use. Most people steer away due to the security issues it causes. Quote Link to comment Share on other sites More sharing options...
Cenron Posted June 6, 2007 Author Share Posted June 6, 2007 What do you think would be the best way to achive what I am trying to do? Quote Link to comment Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 A template engine like ETS or Smarty or store the template files as well php files. Or you can always create the file for instance: $temp_content = implode(file($this->html_code)); $temp_content = str_replace('\\', '\\\\', $temp_content); $temp_content = str_replace('\'', '\\\'', $temp_content); $stemp_content = explode("\n",$temp_content); for($i = 0; $i < sizeof($stemp_content); $i++) { $stemp_content[$i] = 'echo \'' . $stemp_content[$i] . '\' . "\\n";'; } $temp_content = implode("\n",$stemp_content); $filename = 'tempfile' . rand() . time() . '.tpl'; $fp = fopen($filename, 'w+'); fwrite($fp, $temp_content); fclose($fp); ob_start(); echo eval($temp_content); $contents = ob_get_contents(); ob_end_clean(); unlink($filename); return $contents; just remember the <?php and ?> tags are still required. Quote Link to comment Share on other sites More sharing options...
Cenron Posted June 6, 2007 Author Share Posted June 6, 2007 Right on guys thanks for all the help! 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.