Home >Backend Development >PHP Problem >How to modify the content of html file in php

How to modify the content of html file in php

藏色散人
藏色散人Original
2021-07-12 09:16:253925browse

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.

How to modify the content of html file in php

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(&#39;1.html&#39;);
$qiqi=str_replace("abc" , "000" ,$file_content);

$filename = &#39;1.html&#39;;
$somecontent = $qiqi;

// 首先我们要确定文件存在并且可写。
if (is_writable($filename)) {

// 在这个例子里,我们将使用添加模式打开$filename,
// 因此,文件指针将会在文件的开头,
// 那就是当我们使用fwrite()的时候,$somecontent将要写入的地方。
if (!$handle = fopen($filename, &#39;w&#39;)) {
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to install php5.5.16Next article:How to install php5.5.16