php生成shtml类用法实例
本文实例讲述了php生成shtml类及其用法。分享给大家供大家参考。具体如下:
代码如下:
<?php class Shtml{ var $DataSource; //array 数组 var $Templet; //string 字符串 var $FileName; //绑定数据源 function BindData($arr){ $this->DataSource = $arr; } function Create(){ //只谈思路: $tmp = $this->Templet; foreach($this->DataSource as $key=>$value){ //替换模板字符串中<FIELD_$key> 的字符串 $tmp = str_replace('<FIELD_'.$key.'>',$value,$tmp); } //生成文件,存盘。 $fp = fopen($this->FileName,'w'); if (fwrite ($fp,$tmp)){ fclose ($fp); }else { fclose ($fp); } } } //用法如下: $arr = array(); $arr["title"] = "这里是标题"; $arr["content"] = "这里是内容"; $obj = new Shtml; $obj->FileName="xxx.htm"; $obj->Templet="标题:<FIELD_title>内容:<FIELD_content>"; $obj->BindData($arr); //一切OK,万事达吉 $obj->Create(); ?>
希望本文所述对大家的PHP程序设计有所帮助。
相关推荐
聪聪李 2020-09-10
WasteLand 2020-09-15
zyyjay 2020-11-09
xuebingnan 2020-11-05
samtrue 2020-11-22
stefan0 2020-11-22
yifangs 2020-10-13
songshijiazuaa 2020-09-24
hebiwtc 2020-09-18
天步 2020-09-17
83911535 2020-11-13
whatsyourname 2020-11-13
zhouyuqi 2020-11-10
Noneyes 2020-11-10
mathchao 2020-10-28
王志龙 2020-10-28
wwwsurfphpseocom 2020-10-28