当您消除重要的标记为体,头和HTML,你是不是能够看到在浏览器中输出。
因此,str_replace函数正在通过运行下面的代码well.You可以交叉检查:
<?php
$f =file_get_contents('game1/game.html');
$replacetags=array('<html>', '</html>', '<body>', '</body>','<head>','</head>') ;
$file= str_replace($replacetags,"",$f);
file_put_contents("output.html", $file);
?>
运行或加载页面后(即上面的源代码包含)在浏览器中,当你用文本编辑器打开生成的output.html
文件,您将看不到body,head和html标记。
UPD: 为什么HTML文件完全空白?
当你打开的文件或网址与fopen在W¯¯模式,以下情况发生:
'w': Open for writing only; place the file pointer at the
beginning of the file and truncate the file to zero length.
If the file does not exist, attempt to create it.
当你用的fopen为fopen ("game1/game.html", "w");
打开文件时,文件指针被放置在由于您没有在game.html中编写任何内容,所以文件和文件的开头被截断为零(空白),因此html文件变为空白。
还要注意:在str_replace第三个参数是the string or array being searched and replaced on, otherwise known as the haystack
。但在您的代码中,您传递的第三个参数$ f是文件句柄。当你使用输出echo or print
$file
字符串,那么资源ID #x中将被代替印刷过滤(目标)串的结果。
试着看一下[striptags()](http://php.net/manual/en/function.strip-tags.php),除了你必须* *白名单被允许你想要的标签。 –