Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run php code from file_get_contents or file in a function

I am designing my own MVC pattern to ease the process of creating homepages. My templating system needs my controller class to output my views. This means I have to output the file through a php function. I have been searching for some a while now and can't seem to find a solution.

How can I, through a PHP function, run a string representing some source code ("< ?", "< ?php", "? >" and so on) as php? Eval would not take my < ? signs (and I read that function is crap for some reason).

like image 704
Thor A. Pedersen Avatar asked Dec 12 '11 10:12

Thor A. Pedersen


1 Answers

$code = '<?php
function GetBetween($content, $start, $end) {
  $r = explode($start, $content);
  if (isset($r[1])){
    $r = explode($end, $r[1]);
    return $r[0];
  }
  return '';
}';

function _readphp_eval($code) {
  ob_start();
  print eval('?>'. $code);
  $output = ob_get_contents();
  ob_end_clean();
  return $output;
}

print _readphp_eval($code);
like image 153
Meggis Avatar answered Oct 23 '22 09:10

Meggis