simple.init.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. if (!defined('IS_INITPHP')) exit('Access Denied!');
  3. /*********************************************************************************
  4. * InitPHP 3.8.1 国产PHP开发框架 View-simple 简单模板驱动规则模型
  5. *-------------------------------------------------------------------------------
  6. * 版权所有: CopyRight By initphp.com
  7. * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
  8. *-------------------------------------------------------------------------------
  9. * Author:zhuli Dtime:2014-11-25
  10. ***********************************************************************************/
  11. class simpleInit {
  12. /**
  13. * 模板驱动-简单的驱动
  14. * @param string $str 模板文件数据
  15. * @return string
  16. */
  17. public function init($str, $left, $right) {
  18. //if操作
  19. $str = preg_replace( "/".$left."if([^{]+?)".$right."/", "<?php if \\1 { ?>", $str );
  20. $str = preg_replace( "/".$left."else".$right."/", "<?php } else { ?>", $str );
  21. $str = preg_replace( "/".$left."elseif([^{]+?)".$right."/", "<?php } elseif \\1 { ?>", $str );
  22. //foreach操作
  23. $str = preg_replace("/".$left."foreach([^{]+?)".$right."/","<?php foreach \\1 { ?>",$str);
  24. $str = preg_replace("/".$left."\/foreach".$right."/","<?php } ?>",$str);
  25. //for操作
  26. $str = preg_replace("/".$left."for([^{]+?)".$right."/","<?php for \\1 { ?>",$str);
  27. $str = preg_replace("/".$left."\/for".$right."/","<?php } ?>",$str);
  28. //输出变量
  29. $str = preg_replace( "/".$left."(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_$\x7f-\xff\[\]\'\']*)".$right."/", "<?php echo \\1;?>", $str );
  30. //常量输出
  31. $str = preg_replace( "/".$left."([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)".$right."/s", "<?php echo \\1;?>", $str );
  32. //标签解析
  33. $str = preg_replace ( "/".$left."\/if".$right."/", "<?php } ?>", $str );
  34. $pattern = array('/'.$left.'/', '/'.$right.'/');
  35. $replacement = array('<?php ', ' ?>');
  36. return preg_replace($pattern, $replacement, $str);
  37. }
  38. }