如何阅读html文件内容并使用php替换

时间:2013-12-09 10:24:14

标签: php

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

1 个答案:

答案 0 :(得分:1)

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

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