这有什么好的,一两句话就能解决的还用这么复杂?$temp = file_get_contents($fname);
$temp = preg_replace('/\{([0-9a-zA-Z]+)\}/e', '$$1', $temp);

解决方案 »

  1.   

    对,最简单的模板解析不是啊,它生成下面的东西
    $cells = str_replace( "{room_name}", $room_name, $cells );
    $cells = str_replace( "{products_name}", $products_name, $cells );
    $cells = str_replace( "{room_img}", $room_img, $cells );
    $cells = str_replace( "{room_descrip}", $room_descrip, $cells );
    $cells = str_replace( "{products}", $products, $cells );
    $cells = str_replace( "{rooms}", $rooms, $cells );
    $cells = str_replace( "{hotel_name}", $hotel_name, $cells );
    $cells = str_replace( "{school_name}", $school_name, $cells );
    $cells = str_replace( "{hotel_img}", $hotel_img, $cells );
    $cells = str_replace( "{hotel_descrip}", $hotel_descrip, $cells );
    $cells = str_replace( "{school_img}", $school_img, $cells );
    $cells = str_replace( "{school_descrip}", $school_descrip, $cells );
    $cells = str_replace( "{hotels}", $hotels, $cells );
    $cells = str_replace( "{schools}", $schools, $cells );
      

  2.   

    这有什么好的,一两句话就能解决的还用这么复杂?$temp = file_get_contents($fname);
    $temp = preg_replace('/\{([0-9a-zA-Z]+)\}/e', '$$1', $temp);
    这个意思是?
      

  3.   

    Meteorlet(www.dictworld.com) 的有点问题吧,他是直接就将{test}换成$test(寒,用单引号来括起的变量,难道也能让php替换为变量的实际值?应该要用双引号吧)
    不过,他的方法无疑是一个新思路,用一个正规替换代替了楼主的一大堆普通替换
      

  4.   

    能不能替换,有兴趣的人自己去测试,看不懂的人去看看php.net上关于正则表达式中e模式的介绍。
      

  5.   

    哦,$l里存放{xxx}群,用$$l来取出变量
    这样写应该也可以,更容易理解一些。也简洁。$l = array("name","title","descrip");
    $temp = file_get_contents($fname);
    $temp = str_replace("{$l}", $$l, $temp);正则有e模式,受教了。
      

  6.   

    $l是数组,$$l取不出来每个值的,必须循环:
    for($i=0;$i<count($l);$i++)
    {
        $temp = str_replace("{{$l[$i]}}", ${$l[$i]}, $temp);
    }