/ / Vytvoriť HTML súbor s PHP? - php, html

Vytvorte súbor HTML s PHP? - php, html

Chcem vytvoriť súbor html z php súboru, ktorý bude obsahovať tento kód:

<html lang="en">
<head>
<meta http-equiv="refresh" content="0; url=http://example.com/" />
</head>
<body>
</body>
</html>

Čo mám zatiaľ:

$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 = "</headn";
fwrite($myfile, $txt);
$txt = "</html>n";
fwrite($myfile, $txt);
fclose($myfile);

Ale do súboru nič nenapsať.

odpovede:

1 pre odpoveď č. 1

Máte navyše dvojitú citáciu $qq = "/"";, To by malo byť $qq = "/";.

Aj použitie PHP . pre konkatáciu a nie +.

Prečo dont jednoducho používate

$htmlContent = "<html lang="en">
<head>
<meta http-equiv="refresh" content="0; url=http://example.com/" />
</head>
<body>
</body>
</html>"

1 pre odpoveď č. 2

Nie je potrebné používať fwrite ($ myfile, $ txt); znova a znova. Skúste to namiesto toho

<?php

$txt .= "<head>";
$txt .= "</head>";
$txt .= "<body>";
$txt .= "</body>";
$txt .= "</html>";

echo $txt;exit;
?>

0 pre odpoveď č. 3

Môžete vytvoriť nový dokument HTML, ako je tento:

<?php
$dom = new DOMDocument("1.0"); //Create new document with specified version number
echo $dom->saveHTML();        //Outputs the generated source code
?>

Pridávanie prvkov:

<?php
$br = $dom->createElement("br"); //Create new <br> tag
$dom->appendChild($br); //Add the <br> tag to document
?>

0 pre odpoveď č. 4

Nepotrebujete $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);

0 pre odpoveď č. 5

Prvý riadok je nesprávny a zreťazenie reťazca v PHP sa vykonáva s bodkou (".")

$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);