Silently and simply wrote a template engine_PHP tutorial
Engine file
* An OOP template engine developed by Momo based on Discuz template engine, which can support template caching and generate hash md5 values.由hash值来判断模板是否被修改,假如被修改则重新生成缓存文件,假如没有被修改,则直接调用缓存文件.
* 版本:1.0.0.1 beta 测试版
*/
class mmtp{
var $left_tags="{";
var $right_tags="}";
var $tp_suffix=".html";
var $cache_suffix=".tpl";
var $tp_dir="./";
var $cache_dir="./";
/**
* 允许循环嵌套的次数,默认为5
*
* @var unknown_type
*/
var $nest = 5;
/**
* 模板路径
*
* @param unknown_type $tp_dir
* @return mmtp
*/
function __setdir($tp_dir){
if(file_exists($tp_dir)){
$this->tp_dir=$tp_dir;
}else{
$this->error("模板路径不存在");
}
}
/**
* 设置缓存目录
*
* @param unknown_type $cache_dir
*/
function __setcdir($cache_dir){
if(file_exists($cache_dir)){
$this->cache_dir=$cache_dir;
}else{
$this->error("缓存路径不存在");
}
}
/**
* 输出错误信息
*
* @param unknown_type $msg
*/
function error($msg){
print "
}
/** */
function tp($file){
$tp_path=$this->tp_dir.$file.$this->tp_suffix;
$fp=fopen($tp_path,"rb");
if(!$this->file_test($tp_path,"r") && !$this->match_hash($file)){
$template=$this->file_read($tp_path);
$var_regexp = "((\$[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*)([[a-zA-Z0-9_-."'[]$x7f-xff]+])*)";
$const_regexp = "([a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*)";
$template = preg_replace("/([nr]+)t+/s", "\1", $template);
$template = preg_replace("//s", "{\1}", $template);
$template = preg_replace("/{langs+(.+?)}/ies", "languagevar('\1')", $template);
$template = str_replace("{LF}", "="\n"?>", $template);
$template = preg_replace("/{(\$[a-zA-Z0-9_[]'"$.x7f-xff]+)}/s", "=\1?>", $template);
$template = preg_replace("/$var_regexp/es", "$this->addquote('=\1?>')", $template);
$template = preg_replace("/==$var_regexp?>?>/es", "$this->addquote('=\1?>')", $template);
$template = preg_replace("/[nrt]*{templates+([a-z0-9_]+)}[nrt]*/is", "n include('".$this->cache_dir."\1".$this->cache_suffix."'); ?>n", $template);
$template = preg_replace("/[nrt]*{templates+(.+?)}[nrt]*/is", "n include('".$this->cache_dir."\1".$$this->cache_suffix."'); ?>n", $template);
$template = preg_replace("/[nrt]*{evals+(.+?)}[nrt]*/ies", "$this->stripvtags('n \1 ?>n','')", $template);
$template = preg_replace("/[nrt]*{echos+(.+?)}[nrt]*/ies", "$this->stripvtags('n echo \1; ?>n','')", $template);
$template = preg_replace("/[nrt]*{elseifs+(.+?)}[nrt]*/ies", "$this->stripvtags('n } elseif(\1) { ?>n','')", $template);
$template = preg_replace("/[nrt]*{else}[nrt]*/is", "n } else { ?>n", $template);
for($i = 0; $i nest; $i++) {
$template = preg_replace("/[nrt]*{loops+(S+)s+(S+)}[nr]*(.+?)[nr]*{/loop}[nrt]*/ies", "$this->stripvtags('n if(is_array(\1)) { foreach(\1 as \2) { ?>','n\3n } } ?>n')", $template);
$template = preg_replace("/[nrt]*{loops+(S+)s+(S+)s+(S+)}[nrt]*(.+?)[nrt]*{/loop}[nrt]*/ies", "$this->stripvtags('n if(is_array(\1)) { foreach(\1 as \2 => \3) { ?>','n\4n } } ?>n')", $template);
$template = preg_replace("/[nrt]*{ifs+(.+?)}[nr]*(.+?)[nr]*{/if}[nrt]*/ies", "$this->stripvtags('n if(\1) { ?>','n\2n } ?>n')", $template);
}
$template = preg_replace("/{$const_regexp}/s", "=\1?>", $template);
$template = preg_replace("/ ?>[nr]* /s", " ", $template);
$hash=$this->file_hash($tp_path);
$head_hash="";
$foot_time="";
$this->file_write($this->cache_dir.$file.".tpl",$head_hash.$template.$foot_time);
}
}
/**
* Check if the file exists and has read permissions
* *
* @param unknown_type $path
*/
function file_test($path,$method){
if(!file_exists($path) || !fopen($path,$method)){
$this->error("模板文件不存在,或没有操作权限");
return false;
}
}
/** */
function file_read($path,$length=0){
if(!$this->file_test($path,"r+")){
$fp=@fopen($path,"r+");
if($length==0){
$contents=@fread($fp,filesize($path));
}else{
$contents=@fread($fp,$length);
}
fclose($fp);
return $contents;
}
}
/**
*Write file content
*/
function file_write($path,$puts){
if(!$this->file_test($path,"w+")){
$fp=@fopen($path,"w+");
@fwrite($fp,$puts);
fclose($fp);
}
}
/** */
function file_hash($path){
return md5_file($path);
}
/** */
function match_hash($file){
$read_hash=$this->file_read($this->cache_dir.$file.$this->cache_suffix,46);
$html_hash=$this->file_hash($this->tp_dir.$file.$this->tp_suffix);
if(preg_match("/".$html_hash."/i",$read_hash)){
return true;
}
}
function addquote($var) {
return str_replace("\"", """, preg_replace("/[([a-zA-Z0-9_-.x7f-xff]+)]/s", "['\1']", $var));
}
function transamp($str) {
$str = str_replace('&', '&', $str);
$str = str_replace('&', '&', $str);
$str = str_replace('"', '"', $str);
return $str;
}
function stripvtags($expr, $statement) {
$expr = str_replace("\"", """, preg_replace("/=(\$.+?)?>/s", "\1", $expr));
$statement = str_replace("\"", """, $statement);
return $expr.$statement;
}
}
$tp=new mmtp();
$tp->__setdir("./");
$tp->__setcdir("./cache/");
$tp->tp("index1");
$_GET[it]=sdhkadajksdhajdhkajsdhjkasdjkasdhasjdhkjsadhk;
$name=2;
$head="欢迎使用MoMo模板引擎";
include("./cache/index1.tpl");
?>
模板index.html
复制代码
{$head}
模板index1.html
你好
{else}
This template was written silently this afternoon. It was written in a hurry and may have loopholes. This version is only a beta version. I will gradually improve it in the future and send it out first as a preview.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

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),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.