PHP create page as a string after PHP runs

Posted by John on Stack Overflow See other posts from Stack Overflow or by John
Published on 2012-12-06T19:54:15Z Indexed on 2012/12/06 23:04 UTC
Read the original article Hit count: 198

Filed under:
|

I'm stuck on how to write the test.php page result (after php has run) to a string:

testFunctions.php:

<?php

function htmlify($html, $format){
    if ($format == "print"){


        $html = str_replace("<", "&lt;", $html);
        $html = str_replace(">", "&gt;", $html);
        $html = str_replace("&nbsp;", "&amp;nbsp;", $html);
        $html = nl2br($html);
        return $html;
  }
};

$input = <<<HTML
<div style="background color:#959595; width:400px;">
&nbsp;<br>
input <b>text</b>
<br>&nbsp;
</div>
HTML;

function content($input, $mode){
  if ($mode =="display"){
    return $input;
  }
  else if ($mode =="source"){
    return htmlify($input, "print");
  }; 

};

function pagePrint($page){

  $a = array(
    'file_get_contents' => array($page),
    'htmlify' => array($page, "print")
  );  
  foreach($a as $func=>$args){
      $x = call_user_func_array($func, $args);
      $page .= $x;
  }    
  return $page;
};


$file = "test.php";
?>

test.php:

<?php include "testFunctions.php"; ?>


<br><hr>here is the rendered html:<hr>

<?php $a = content($input, "display"); echo $a; ?>

<br><hr>here is the source code:<hr>

<?php $a = content($input, "source"); echo $a; ?>


<br><hr>here is the source code of the entire page after the php has been executed:<hr>
<div style="margin-left:40px; background-color:#ebebeb;">
<?php $a = pagePrint($file); echo $a; ?>
</div>

I'd like to keep all the php in the testFunctions.php file, so I can place simple function calls into templates for html emails.

Thanks!

© Stack Overflow or respective owner

Related posts about php

Related posts about function