Solution 1 :

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.")
?>

Problem :

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

Comments

Comment posted by jibsteroos

undefined variable

Comment posted by zyrius

Thank you for your help

By