日期:2014-05-17  浏览次数:20393 次

str_replace不执行
代码:
$read_str = file_get_contents("../template/HTMLPage.htm");//这是网页模板
$read_str = str_replace("{标题}","111",$read_str);
$read_str = str_replace("{内容}","222",$read_str);
echo($read_str);
最后输出的还是原模板内容,标题和内容没有被替换,但是模板的内容已经读出来了,注释掉中间两行的话可以输出模板内容。如果第一行换成直接$read_str=模板内容,就可以替换,为什么从文件读取出来的就不行呢?

------解决方案--------------------
error_reporting(E_ALL)试试?看看有没错误?
------解决方案--------------------
给出模板文件.
------解决方案--------------------
经过测试这样是没问题的,如果你这样测试也没问题的话,可能出在文件的编码上。确认文件的编码都一致
PHP code

$read_str='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>模板</title>
</head>
<body style="margin:0">
<input type="hidden" id="hd_nid" />
<table align="center" width="960px" cellpadding="0" cellspacing="0" border="1">
  <tr>
  <td style="height:50px;text-align:center;font-size:18px;font-weight:bold">{标题}</td>
  </tr>
  <tr>
  <td style="font-size:14px;line-height:25px">{流量}</td>
  </tr>
  <tr>
  <td style="font-size:14px;line-height:25px">{内容}</td>
  </tr>
  <tr>
  <td style="font-size:14px;line-height:25px">{分页}</td>
  </tr>
</table>
</body>
</html>';
$read_str = str_replace("{标题}","111",$read_str);
$read_str = str_replace("{内容}","222",$read_str);
echo($read_str);

------解决方案--------------------
探讨
如果第一行换成直接$read_str=模板内容,就可以替换,为什么从文件读取出来的就不行呢?

------解决方案--------------------
你将哪些模版标签换成英文的试试?
------解决方案--------------------
应该是编码不一致。经测试,没有发现你说的问题。
------解决方案--------------------
两个文件的编码要一致。 在编辑器里面改。
------解决方案--------------------
gbk也行。 但是两个文件需要统一。再加个header 输出时避免乱码。
------解决方案--------------------
探讨

统统保存为utf-8的格式就好了,不过我用的phpDesigner7里点另存为没有格式的选择,我用EditPlus保存的,不过没有gb2312格式,是不是中文格式只能保存为utf-8