Home >Backend Development >PHP Problem >How to modify the content of html file in php
How to modify the content of the html file in php: first make sure the file exists and is writable; then modify the html file through "if (fwrite($handle, $somecontent) === FALSE) {...}" Just the content.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
How does php modify the content of the html file?
The code is as follows:
<?php $file_content = file_get_contents('1.html'); $qiqi=str_replace("abc" , "000" ,$file_content); $filename = '1.html'; $somecontent = $qiqi; // 首先我们要确定文件存在并且可写。 if (is_writable($filename)) { // 在这个例子里,我们将使用添加模式打开$filename, // 因此,文件指针将会在文件的开头, // 那就是当我们使用fwrite()的时候,$somecontent将要写入的地方。 if (!$handle = fopen($filename, 'w')) { echo "不能打开文件 $filename"; exit; } // 将$somecontent写入到我们打开的文件中。 if (fwrite($handle, $somecontent) === FALSE) { echo "不能写入到文件 $filename"; exit; } echo "成功地将 $somecontent 写入到文件$filename"; fclose($handle); } else { echo "文件 $filename 不可写"; } ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to modify the content of html file in php. For more information, please follow other related articles on the PHP Chinese website!