Hi, I have tried tons of functions but none works the way I want. I have a page foo.php. Then in that I set a couple of variables. Now, if I include foo.inc.php that file can use all of these variables. Example of foo.inc.php could be:

---foo.inc.php---

<div>
<span style="font-weight: bold; margin-right: 20px"><?=$forum_dep?></span>
<span style="margin-right: 20px">TOPICS</span>
<span style="margin-right: 20px">POSTS</span>
<span style="margin-right: 20px">LAST POST</span>
</div>

---/foo.inc.php---

But now I first wish to put the contents of the included file in a variable and pass that to a function that takes a string and just puts it in a nice rounded frame. But if I use file_get_contents the php is not proberly executed. So I looked at ways to use eval to get it done. But it always seems like the executed code comes first and then the frame underneeth. Probably beacuse the phpcode is written directly once it's executed. So I looked at php.net under the eval function and found this.

function eval_mixed_helper($arr){
  return ("echo stripslashes(\"".addslashes($arr[1])."\");");
  }

function eval_mixed($string){
  $string = "<? ?>".$string."<? ?>";
  $string = preg_replace("/<\?=\s+(.*?)\s+\?>/", "<? echo $1; ?>", $string);
  $string = str_replace('?>', '', str_replace( array('<?php', '<?'), '', preg_replace_callback( "/\?>((.|\n)*?)<\?/","eval_mixed_helper",$string) ) );
  return eval($string);
  }

But if I try that using

// output to variable
	ob_start();
	eval_mixed(file_get_contents(root_path . 'foo.inc.php'));
	$final_html = ob_get_clean();

msgBox($final_html);

it still behaves strange. I get eval error. If I change foo.inc.php to this it works.

<?
$forum_dep = $GLOBALS['forum_department_name'];
?>
<div>
<span style="font-weight: bold; margin-right: 20px">$forum_dep</span>
<span style="margin-right: 20px">TOPICS</span>
<span style="margin-right: 20px">POSTS</span>
<span style="margin-right: 20px">LAST POST</span>
</div>

But that kind of spoils the intire idea of why I wanted to get a function like this to work. Anyone have an idea on how to get this to work?

    Is there any reason you couldn't just include() the file?

      If I include the file and want to use it in my msgBox function I would have to do

      msgBox(include(foo.inc.php));

      But that makes the content of foo.inc.php appear before the code of the msgBox thus rendering it outside and above the box.

        How about, before the foo function somewhere,

        ob_start();
        include('file.inc.php');
        $holder = ob_get_contents();
        ob_end_clean();
        

        And now $holder has the parsed code in it's hands, which you can use as you like.

          I will try that... Lol... How simple if it works. I have banged my head against the wall a couple of times with this 😛

            Worked like a charm. Havent really used that ob_start() and ob_getcontents before. Is it possible to use several ob_starts in a nested fashion and outputing everything at the end? To avoid problems with "headers already sent" that happens with some function?

            Thanx again for the solution.

              No. The output buffer is linear and held in memeory. But, it's possible to have multiple output buffers in the same file at different times, store the outputs in different variables, and echo/print them at the end in any order.

                DanielLiljeberg wrote:

                Is it possible to use several ob_starts in a nested fashion and outputing everything at the end?

                Yes.

                [man]ob_start[/man]
                Output buffers are stackable, that is, you may call ob_start() while another ob_start() is active. Just make sure that you call ob_end_flush() the appropriate number of times. If multiple output callback functions are active, output is being filtered sequentially through each of them in nesting order.

                  Write a Reply...