Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

Problem with entry in an html file via php

good evening,

My problem is I try to use PHP to replace "hello world" in an HTML file, but each time I only manage to empty the file completely. can anyone tell me what I'm doing wrong?

here the HTML and PHP file

<!DOCTYPE html>
    <html lang="de">
    <head>
        <style> @page {size: 793px 1122px;margin: 0px;padding: 0px;}
        * { box-sizing: border-box; } 
        body {margin: 0;}
        #iht9{left:149px;top:214px;position:absolute;}
        #i1tz{left:149px;top:116px;position:absolute;}
        #ivrl{left:155.5px;top:149px;position:absolute;}
        </style>
    </head>
    <body>
            <h2 id="iht9">hi</h2>
            <h2 id="ivrl">Hallo Welt!!!</h2>
    </body>
</html>

<?php
$msg= "wuff wuff";


$file= file("hase.html");
$file = str_replace("$msg",$file);

$fp = fopen ("hase.html", "w+") or die("Fehler beim ?ffnen");  

foreach ($file as $line)
fputs($fp,$line);

fclose($fp);
?>
question from:https://stackoverflow.com/questions/65602411/problem-with-entry-in-an-html-file-via-php

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you can try this, i hope that's what exactly you need

<!DOCTYPE html>
    <html lang="de">
    <head>
        <style> @page {size: 793px 1122px;margin: 0px;padding: 0px;}
        * { box-sizing: border-box; } 
        body {margin: 0;}
        #iht9{left:149px;top:214px;position:absolute;}
        #i1tz{left:149px;top:116px;position:absolute;}
        #ivrl{left:155.5px;top:149px;position:absolute;}
        </style>
    </head>
    <body>
            <h2 id="iht9">Line1</h2>
            <h2 id="ivrl">Line2</h2>
            <h2 id="i1tz">Line3</h2>
    </body>
</html>

<?php
//$filename=$_GET['file_name']; // To get the file name from URL 
$filename='test.php'; // file name is stored

$fd = fopen ($filename, "r"); // opening the file in read mode
$contents = fread ($fd, filesize($filename)); // reading the content of the file
fclose ($fd);                                // closing the file pointer
$search=array("Line1","Line2","Line3");
$replace=array("Newline -> (1)","Newline -> (2)","Newline -> (3)");
$contents=str_replace($search,$replace,$contents);

$fd = fopen ($filename, "w"); // opening the file in write mode
//echo $contents;               // printing the file content of the file
fwrite ($fd,$contents);        // entering data to the file
chmod($filename,0777);       // Setting the write permission 
fclose ($fd);               // closing the file pointer
echo ("all done refresh the page now to see result.")
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...