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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.