2013-04-08 67 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()); 
?>