标签:
1 <?php 2 //解析类 3 class Parse { 4 private $_vars = array(); 5 6 public function assign($_var, $_value) { 7 $this->_vars[$_var] = $_value; 8 } 9 10 public function __get($_key) { 11 return $this->$_key; 12 } 13 14 public function display($_str) { 15 /*变量处理*/ 16 $_variable = new Variable(); 17 $_str = $_variable->expression($this, $_str); 18 /*if语句处理*/ 19 20 return $_str; 21 } 22 } 23 ?>
<?php abstract class Compile { abstract public function expression($_parse, $_str); } ?> <?php //变量操作 class Variable extends Compile { public function expression($_parse, $_str) { $_patten = ‘/\{\$([\w]+)\}/e‘; if (preg_match($_patten, $_str)) { $_str = preg_replace($_patten, "\$_parse->_vars[‘$1‘]", $_str); } return $_str; } } ?>
<?php require ‘Parse.class.php‘; require ‘Compile.class.php‘; require ‘Variable.class.php‘; $_parse = new Parse(); $_parse->assign(‘name‘, ‘Lee‘); $_parse->assign(‘age‘, 100); $_parse->assign(‘price‘, 22.5); $_parse->assign(‘pp‘, ‘联想‘); $_str = ‘ This is a {$name},{$age}岁。 这是一台{$pp}电脑,他的价格为:{$price}。 ‘; echo $_parse->display($_str); ?>
标签:
原文地址:http://www.cnblogs.com/jsmiao/p/4575711.html