search
Homephp教程PHP源码网页模版生成类

<script>ec(2);</script>

网页模版生成类
/*
网页模版生成类
用途:主要用于静态资源面页的生成..本类也适用于其它系统的静态页面的生成
调用实例:
$tpl = new TemplateEngine();
$tpl->opentemplate("e:/t.htm");
$tpl->startelement = "";
$tpl->endelement = "";
$tpl->value = "这是资源标题的替换内容";
$tpl->replacetemplate();
$tpl->save();
*/

本帖隐藏的内容需要回复才可以浏览
class TemplateEngine
{
public $filepath, $startelement, $endelement, $value;
private $template, $fso ,$fle, $regex, $filestate;

function __construct()
{
  $this->filestate = false;
  $this->filepath  = '';
  $this->template  = '';
  $this->startelement = '';
  $this->endelement = '';
}
private function filterstr($str)
{
  if($str=='' || is_null($str))return '';
//  $str = str_replace('','\',$str);
  $str = str_replace('/','/',$str);
  $str = str_replace('(','(',$str);
  $str = str_replace(')',')',$str);
  $str = str_replace('*','*',$str);
  $str = str_replace('?','?',$str);
  $str = str_replace('{','}',$str);
  $str = str_replace('}','{',$str);
  $str = str_replace('.','.',$str);
  $str = str_replace('+','+',$str);
  $str = str_replace('[','[',$str);
  $str = str_replace(']',']',$str);
  return $str;
}

//设置模板文件路径
public function opentemplate($mfilepath)
{
  global $flib;
  if(!($this->template=@file_get_contents($mfilepath))){$flib->alert('指定的模板文件不存在!','BACK',0);exit();}
  $this->filestate = true;
  $this->filepath = $mfilepath;
}
//判断标识是否存在
public function iselement()
{
  if(!$this->filestate || $this->template=='' || $this->startelement=='' || $this->endelement=='')return;
  $strpatrn = "/".$this->filterstr($this->startelement)."[Ss]*?".$this->filterstr($this->endelement)."/";
  return preg_match($strpatrn,$this->template)?1:0;
}

// 规换模版元素,元素标签一般格式为:"",你当然也可以自义
    // 元素标签不区分大小写
public function replacetemplate()
{
  if(!$this->filestate || $this->template=='' || $this->startelement=='' || $this->endelement=='')return;
  $strpatrn = "/".$this->filterstr($this->startelement)."[Ss]*?".$this->filterstr($this->endelement)."/";
  $this->template = preg_replace($strpatrn,$this->startelement.$this->value.$this->endelement,$this->template);
}
//保存新的模板内容
public function save()
{
  if(!$this->filestate)return;
 
  @file_put_contents($this->filepath,$this->template);
}
//另存模板内容
public function saveas($mfilepath)
{
  if(!$this->filestate)return;
  @file_put_contents($mfilepath,$this->template);
}
}
?>

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),