我想从包含以下代码的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", "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);
但是它没有写任何文件。
答案 0 :(得分:1)
$qq = "/"";
中有额外的双引号。它应该是$qq = "/";
。
此外,PHP使用.
进行连接,而不是+
。
为什么不使用
$htmlContent = '<html lang="en">
<head>
<meta http-equiv="refresh" content="0; url=http://example.com/" />
</head>
<body>
</body>
</html>'
答案 1 :(得分:1)
无需使用fwrite($ myfile,$ txt);一次又一次。试试这个
<?php
$txt .= "<head>";
$txt .= "</head>";
$txt .= "<body>";
$txt .= "</body>";
$txt .= "</html>";
echo $txt;exit;
?>
答案 2 :(得分:0)
您可以像这样创建一个新的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
?>
答案 3 :(得分:0)
您不需要$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);
答案 4 :(得分:0)
第一行是错误的,PHP中字符串的串联是用点(“。”)完成的
$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);