用PHP创建HTML文件?
我想从包含以下代码的php文件创建一个html文件:用PHP创建HTML文件?,php,html,Php,Html,我想从包含以下代码的php文件创建一个html文件: <html lang="en"> <head> <meta http-equiv="refresh" content="0; url=http://example.com/" /> </head> <body> </body> </html> 到目前为止,我所拥有的: $qq = "/""; $myfile = fopen("mlgtest.html",
<html lang="en">
<head>
<meta http-equiv="refresh" content="0; url=http://example.com/" />
</head>
<body>
</body>
</html>
到目前为止,我所拥有的:
$qq = "/"";
$myfile = fopen("mlgtest.html", "w")or die("Unable to open file!");
$txt = "<html>\n";
fwrite($myfile, $txt);
$txt = "<head>\n";
fwrite($myfile, $txt);
$txt = "<meta http-equiv=" + $qq + "refresh" + $qq + "content=" + $qq + "0; url="+ $qq + "http://example.com/" + $qq + ">/n";
fwrite($myfile, $txt);
$txt = "</head\n";
fwrite($myfile, $txt);
$txt = "</html>\n";
fwrite($myfile, $txt);
fclose($myfile);
$qq=“/”;
$myfile=fopen(“mlgtest.html”,“w”)或die(“无法打开文件!”);
$txt=“\n”;
fwrite($myfile,$txt);
$txt=“\n”;
fwrite($myfile,$txt);
$txt=“/n”;
fwrite($myfile,$txt);
$txt=“您在$qq=“/”;
中有一个额外的双引号。它应该是$qq=“/”;
PHP也使用
进行压缩,而不是+
你为什么不干脆用它呢
$htmlContent = '<html lang="en">
<head>
<meta http-equiv="refresh" content="0; url=http://example.com/" />
</head>
<body>
</body>
</html>'
$htmlContent='1!'
'
您可以创建一个新的HTML文档,如下所示:
<?php
$dom = new DOMDocument('1.0'); //Create new document with specified version number
echo $dom->saveHTML(); //Outputs the generated source code
?>
添加元素:
<?php
$br = $dom->createElement('br'); //Create new <br> tag
$dom->appendChild($br); //Add the <br> tag to document
?>
您不需要$qq
$myfile = fopen("mlgtest.html", "w")or die("Unable to open file!");
$txt = "<html>\n";
fwrite($myfile, $txt);
$txt = "<head>\n";
fwrite($myfile, $txt);
$txt = '<meta http-equiv="refresh" content="0; url=http://example.com/" />/n'; // notice using ' not "
fwrite($myfile, $txt);
$txt = "</head>\n";
fwrite($myfile, $txt);
$txt = "</html>\n";
fwrite($myfile, $txt);
fclose($myfile);
$myfile=fopen(“mlgtest.html”,“w”)或die(“无法打开文件!”);
$txt=“\n”;
fwrite($myfile,$txt);
$txt=“\n”;
fwrite($myfile,$txt);
$txt='/n';//注意使用“not”
fwrite($myfile,$txt);
$txt=“\n”;
fwrite($myfile,$txt);
$txt=“\n”;
fwrite($myfile,$txt);
fclose($myfile);
无需使用fwrite($myfile,$txt);一次又一次。试试这个
<?php
$txt .= "<head>";
$txt .= "</head>";
$txt .= "<body>";
$txt .= "</body>";
$txt .= "</html>";
echo $txt;exit;
?>
第一行是错误的,PHP中字符串的串联是用点(“.”)完成的
$qq=“/”;
$myfile=fopen(“mlgtest.html”,“w”)或die(“无法打开文件!”);
$txt=“\n”;
$txt.=“\n”;
$txt.=“/n”;
$txt.=“\n”;
$txt.=“\n”;
fwrite($myfile,$txt);
fclose($myfile);
它应该是双引号,否则会给我一个错误。我认为应该在删除额外的双引号后重试。我可以在问题中清楚地看到,文本颜色与它应该的颜色相反。红色是黑色,黑色是红色。你有什么错误信息?请你解释一下目的。此外,您确实意识到,您可以简单地在php文件中像平常一样编写HTML,只是不要在其周围使用php标记。然后将php封装在标记中:)如果您想生成一个文件,最好使用新的DOMDocument();选项
$qq = "/";
$myfile = fopen("mlgtest.html", "w")or die("Unable to open file!");
$txt = "<html>\n";
$txt .= "<head>\n";
$txt .= "<meta http-equiv=" . $qq . "refresh" . $qq . "content=" . $qq . "0; url=". $qq . "http://example.com/" . $qq . ">/n";
$txt .= "</head>\n";
$txt .= "</html>\n";
fwrite($myfile, $txt);
fclose($myfile);