<?php class Parser { private $_tpl; public function __construct($_tplFile) { if (! $this->_tpl = file_get_contents($_tplFile)) { exit('ERROR:模版文件读取错误'); } } private function parvar() { $_patten = '/<!--\s+\{$([\w]+)\}\s+-->/'; if (preg_match($_patten,$this->_tpl)) { $this->_tpl = preg_replace($_patten, "<?php echo $this->_vars[''];?>",$this->_tpl); } } private function parif(){ $_pattenif = '/<!--\s+\{if\s+$([\w]+)\}\s+-->/'; $_pattenElse = '/<!--\s+\{else\}\s+-->/'; $_pattenEndif = '/<!--\s+\{\/if\}\s+-->/'; if (preg_match($_pattenif,$this->_tpl)) { if (preg_match($_pattenEndif,$this->_tpl)) { $this->_tpl = preg_replace($_pattenif,"<?php if ($this->_vars['']){?>",$this->_tpl); $this->_tpl = preg_replace($_pattenEndif,"<?php } ?>",$this->_tpl); if (preg_match($_pattenElse,$this->_tpl)) { $this->_tpl = preg_replace($_pattenElse,"<?php }else{?>",$this->_tpl); } }else{ echo 'ERROR:IF语句没有关闭!'; } } }
After receiving the content of the template file, construct a method, obtain the content of the template file and parse it, use ordinary variables to parse the IF statement, and then parse the template file to generate a compiled file.
All resources on this site are contributed by netizens or reprinted by major download sites. Please check the integrity of the software yourself! All resources on this site are for learning reference only. Please do not use them for commercial purposes. Otherwise, you will be responsible for all consequences! If there is any infringement, please contact us to delete it. Contact information: admin@php.cn
Related Article
13Jul2016
PHP template parsing class instance, php template instance. PHP template parsing class examples, php template examples This article describes the PHP template parsing class. Share it with everyone for your reference. The details are as follows: phpclass template { private $vars = array(); p
13Jul2016
PHP creates and parses XML. As XML continues to heat up, PHP provides a large number of standard libraries (class libraries) specifically designed to handle XML to manipulate XML. For example, DOM XML extension rewriting, SimpleXML extension, SOAP extension
13Jul2016
24 useful PHP libraries to share, 24php libraries. 24 useful PHP libraries to share, 24php libraries Currently, PHP is the most popular scripting language used for web development. You can easily find a lot of information about PHP on the Internet, including documentation, tutorials,
14Jul2016
PHP template parsing example. Below is an example of PHP template parsing. Convert {youku id_XMTc5ODgwOTA4} to the corresponding playback code through regular expressions. ?php header(Content-type:text/html; charset=utf-8); $te
13Jul2016
15 very practical open source PHP libraries, 15 open source libraries. 15 very practical open source PHP class libraries, 15 open source class libraries The PHP library provides developers with a standard interface, which helps developers make full use of object-oriented programming in PHP. These libraries are specific
Hot Tools
PHP library for dependency injection containers
PHP library for dependency injection containers
A collection of 50 excellent classic PHP algorithms
Classic PHP algorithm, learn excellent ideas and expand your thinking
Small PHP library for optimizing images
Small PHP library for optimizing images