0

test.php ページの結果 (php の実行後) を文字列に書き込む方法に行き詰まっています。

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>

すべての php を testFunctions.php ファイルに保持したいので、単純な関数呼び出しを HTML メールのテンプレートに配置できます。

ありがとう!

4

3 に答える 3

0

私の元の方法は、最善の方法ではなかったようです。同じトピックについて新しい質問をする代わりに、別の方法を提供して、それが私が求めている解決策につながるかどうかを確認する方がよいと考えました.

testFunctions.php:

$content1 = "WHOA!";
$content2 = "HEY!";
$file = "test.html";

$o = file_get_contents('test.html');

$o = ".$o.";

echo $o;

?>

text.php:

<hr>this should say "WHOA!":<hr>

$content1

<br><hr>this should say "HEY!":<hr>

$content2

私は基本的に$oにtest.phpファイルの文字列を返そうとしていますが、php変数を解析したいと思っています。あたかも次のように読まれたかのように:

$o = "
    <html>$content1</html>
";

また

$o = <<<HTML
<html>$content1</html>
HTML;

ありがとう!

于 2012-12-06T23:01:13.210 に答える
0

出力バッファリングを使用して、インクルード ファイルの出力をキャプチャし、変数に割り当てることができます。

function pagePrint($page, array $args){
 extract($args, EXTR_SKIP);
 ob_start();
 include $page;
 $html = ob_get_clean();
 return $html;
}

pagePrint("test.php", array("myvar" => "some value");

そしてtest.php

<h1><?php echo $myvar; ?></h1>

出力します:

<h1>some value</h1>
于 2012-12-06T20:05:26.520 に答える
0

これはまさにあなたが探しているものではないかもしれませんが、php 関数を入れることができる電子メール テンプレートを処理するためのエンジンを構築したいと思われますか? http://phpsavant.com/をチェックしてみてください。これは、基本的な変数の割り当てだけでなく、php 関数をテンプレート ファイルに直接配置できるシンプルなテンプレート エンジンです。

printPage が何をしているのかわかりませんが、関数呼び出しの配列が少し複雑で、実際に起こっていることはこれだけだと思う​​ので、わかりやすくするために次のように書き直します。

function pagePrint($page) {
    $contents = file_get_contents($page);
    return $page . htmlify($contents,'print');
};

htmlify() 関数を取り除き、組み込み関数 htmlentities() または htmlspecialchars() のいずれかを使用することを検討するかもしれません。

于 2012-12-06T20:13:40.757 に答える