如何读取html文件内容并使用php进行替换


how to read the html file contents and replace using php

$file_handle    = fopen($htmlFileName,"r+");
while (!feof($file_handle)) 
{
    $templateData = fgets($file_handle);
    str_replace("#softwareVersion#",'Data',$templateData);
}

为什么不使用简单的方法呢?

$data = file_get_contents($htmlFileName); // You are reading here
$replacedCont = str_replace("#softwareVersion#",'Data',$data); // You are replacing the content
file_put_contents($htmlFileName,$replacedCont);// You are writing the replaced content to the file.

编辑:

我想用PHP 替换这么多html页面的字符串

你的问题没有提到这一点。您可以使用要替换的数据数组。像这样做。。。

$content_to_be_replaced = array('#softwareVersion#','#software#','#hardware#');
$content_to_be_replaced_with = array($exportMetaData['params']['softwar‌​eVersion'],$exportMetaData['params']['software']‌​,$exportMetaData['params']['hardware']);
$total_replaced_content = str_replace($content_to_be_replaced,$content_to_be_replaced_with,$data);