Php echo整页HTML覆盖


Php echo whole page HTML override

我试图使特定网页"回显"一个全新的HTML页面,同时"覆盖"请求页面"中设置的任何内容。假设它有某种形式的内容,我希望"回声"页面完全取代它、<head>标签和所有内容
类似的概念(据我所知)被各种模板引擎使用。然而,他们使用结构化的HTML并用数据填充它,同时我希望在整个文件中动态创建HTML结构,这些结构将被聚合成一个字符串,然后"创建"成HTML显示给用户。

<?php
    $htmlcontent = "'n";
    $htmlcontent .= "<html>'n";
    $htmlcontent .= "<head>'n";
    $htmlcontent .= "<style> body {'n";
    $htmlcontent .= "            background-color: red;'n";
    $htmlcontent .= "        }'n";
    $htmlcontent .= "</style>'n";
    $htmlcontent .= "</head>'n";
    $htmlcontent .= "<body>'n";
    $htmlcontent .= "<div style='"background-color:yellow; width:200px; height:200px; position:absolute'"> Some text </div>'n";
    $htmlcontent .= "<script>'n";
    $htmlcontent .= "alert('"script has run!'");'n";
    $htmlcontent .= "</script>'n";
    $htmlcontent .= "</body>'n";
    $htmlcontent .= "</html>'n";
    echo ($htmlcontent); //EDIT: removed eval() from here
?>

EDIT:删除了eval()以避免混淆新用户,为阅读文章评论的用户添加评论,以免因代码中缺少eval而混淆

这是我的请求所针对的.php页面的内容。这个想法是,为了能够在浏览器中获得一个全新的页面,所发生的是一个错误(现在无关紧要,因为Eval被删除了):

分析错误:语法错误,意外的'<'在里面D: ''wamp''www''learningPHP''index.php(30):第2行上的eval()代码

如果我只是使用:

echo $htmlcontent;

然后我让页面打印出我的HTML结构,但它都打印在一个空白HTML页面的主体中,而不是按照我的需要进行结构化。

答案:

问题出在NetBeans的一个插件上。从服务器手动编辑.php文件时,它似乎携带了文档的标准HTML标记,以及一些注释。删除这些修复了问题。

我阅读了你的问题和你的所有评论。

我想你需要ob_start();,它很简单:

ob_start(); //write this line in start of your project, 
//In header file. 
//Here is normal html/css/js/PHP. write your html or echo any things...
// Normal code is here....
//e.g: 
<html><body>Hello world <?php date("y"); ?> </body></html>
$html = ob_get_clean(); //write this line at the end of file.. in footer file
//now your html save in $html variable,
now use replace functions to replace any thing from your html.. :)
//at the end, 
echo $html;

ob_start();当您使用此功能时,在您回显结果之前,浏览器上不会显示任何内容

$html = ob_get_clean();
echo $html;