Jump to content

[SOLVED] Reading from a variable instead of a file


Cenron

Recommended Posts

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?

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.

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;

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.

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.