<?php
$link=mysql_connect("127.0.0.1","root","2000");
$db=mysql_select_db("tt");
mysql_query("set names gbk"); 
$result=mysql_query("select * from user",$link);
mysql_close($link);
$filename='index.html';$info= fso($filename);
while($rs=mysql_fetch_assoc($result))
{
$info2=str_replace('{$id}',$rs["id"],whiletags($filename));
$info2=str_replace('{$username}',$rs["username"],$info2);
$info2=str_replace('{$userpass}',$rs["userpass"],$info2);
$t=$t.$info2;
}
//echo $t; //循环内容
//echo $info; //模板内容
$infook= ereg_replace('/{\$while}(.*?){\/\$while}/',$t,$info);      //这一行通不过。
echo $infook;
//提取模板内容
function fso($fname)
{
$f=fopen($fname,"r");
$p=fread($f,filesize($fname));
$p=str_replace("\r\n","",$p);
return $p;
fclose($f);
}//循环标签
function whiletags($fname)
{
$whileinfo=str_replace(" ","",fso($fname));
preg_match_all('/{\$while}(.*?){\/\$while}/',$whileinfo,$info);
return $info[1][0];
}
?>
index.html<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>模板研究</title>
</head><body>
<table width="200" border="1">
  <tr>
    <td>编号</td>
    <td>用户名</td>
    <td>密码</td>
  </tr>
  {$while}
  <tr>
    <td>{$id}</td>
    <td>{$username}</td>
    <td>{$userpass}</td>
  </tr>
  {/$while}
</table>
</body>
</html>