アプリ版:「スタンプのみでお礼する」機能のリリースについて

PHPでヒアドキュメントの中で別ファイルをインクルードする方法はどのように処理するのでしょうか?

sample.txt
<p>サンプルテキストからの文字</p>
------------------------------------
sample.php
$sample = <<<_EOM_
<html>
<head>
</head>
<body>
サンプルサンプル<br>
*ここにsample.txt内のhtmlソースをインクルードしたい。
</body>
</html>
_EOM_;

以下省略(echoとかで出力)

A 回答 (3件)

以下の方法で、includeと同じ結果が得られます


<?
$include=file_get_contents("sample.txt");
$sample = <<<_EOM_
<html>
<head>
</head>
<body>
サンプルサンプル<br>
$include
</body>
</html>
_EOM_;

echo $sample;
?>
    • good
    • 0

file_get_contentsなどで変数にいれるのがてっとりばやい



<?
$fname='sample.txt';
$content = file_get_contents($fname);
$sample = <<<_EOM_
<html>
<head>
</head>
<body>
サンプルサンプル<br>
{$content}
</body>
</html>
_EOM_;
print $sample;
?>
    • good
    • 0

出来ないです。



前半のヒアドキュメント、別ファイルを読み込んだ文字列、後半のヒアドキュメントを連結しましょう。
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!