2013-04-08 66 views
0

我的目標是在服務器上自動創建一個具有以下html/php文件的文件 但它包含php包含,因此當頁面構建時它包含包含的所有內容也是如此。不解析的Php文件

我想要include("../includes/right.html");獨立 在生成的頁面,但我希望其他PHP變量進行分析。

<?php 
// Start the buffering // 
ob_start(); 
?> 

<?php echo $name; ?> 

test text line one 

<?php include("../includes/right.html"); ?> 

test text line two 

<?php 
// Putting content buffer into file // 
file_put_contents('mypage.html', ob_get_contents()); 
?> 
+4

退出'<?php include ...?>',這樣PHP就不會將其解釋爲PHP。 –

+0

嘿馬特,不知道你的意思是逃避嗎? – mark

+1

另一個解決方案是將文件作爲字符串通過單獨的PHP腳本讀取,然後將其回顯出來。 – Appleshell

回答

0

這是直接從PHP手冊在http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc 這就是所謂的nowdoc語法,並配備了PHP 5.3。這通常是這樣的最佳做法。

echo <<<'EOT' 
My name is "$name". I am printing some $foo->foo. 
Now, I am printing some {$foo->bar[1]}. 
This should not print a capital 'A': \x41 
EOT; 

'EOT'和EOT之間的任何東西;將作爲文本呈現在頁面上,不需要任何解析。

+0

非常感謝朋友,正是我一直在尋找的東西。感謝其他人以及建議。 – mark

0

您需要將該行轉義爲HTML,以免它通過PHP解析器,例如,

<?php 
// Start the buffering // 
ob_start(); 
?> 

<?php echo $name; ?> 

test text line one 

&lt;?php include(&quot;../includes/right.html&quot;); ?&gt; 

test text line two 

<?php 
// Putting content buffer into file // 
file_put_contents('mypage.html', ob_get_contents()); 
?>