用AI赚第一桶💰低成本搭建一套AI赚钱工具,源码可二开。 广告
下载[https://github.com/smarty-php/smarty/releases/](https://github.com/smarty-php/smarty/releases/) ## **smarty原理:** php加载带有特定规则的html,然后通过正则匹配出特定规则,最后将匹配出的特定规则替换上真实数据 **smarty原理 例子1:** 1.html ``` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h3>{$username}</h3> <h3>{$age}</h3> </body> </html> ``` 1.php ``` <?php // 将1.html页面的具有特殊标示符的变量提取出来,并进行替换操作 // 1.读取1.html文件的内容(将1.html的内容作为字符串读取出来) $info = file_get_contents('1.html');//将html模板文件的内容读取出来 // echo $info; // 2.替换操作{$username}即替换出加载1,html内容的{$username} $preg = '/\{\$[a-zA-Z_][a-zA-Z_0-9]*\}/';匹配出{username}和{$age} $str = preg_replace($preg, 'zhangsan', $info);//将{username}和{$age}都替换成了zhangsan echo $str; ?> ``` **例子2:** Engine.class.php ``` // 模板引擎 class Engine { // 定义存储数据的属性 private $fields; private $template_dir = 'templates/'; // 模板目录 private $compile_dir = 'compile/'; // 编译目录 private $cache_dir = 'cache/'; // 缓存目录 // 分配变量 如:$eng->assign('username','zhangsan'); public function assign($key, $value) { $this->fields[$key] = $value; } /** * 编译模板(template) * 将编译文件放置在compile目录下 * 将模板文件放置在templates目录下 */ public function display($tpl) //传入$tpl模板页的地址 { // 1.读取模板数据 $info = file_get_contents($this->template_dir.$tpl);//读取模板内容里面有准备替换的{$username} // 2.数据替换操作preg_replace($patterns, $replace, $str); $preg = '/\{\$([a-zA-Z_][a-zA-Z_0-9]*)\}/'; //$preg可能会匹配很多[模版变量 ]他是一个数组{$username} //echo preg_replace($preg,'\\1', $info); 替换成了username $replace = "<?php echo \$this->fields['\\1']; ?>"; // \\1 匹配出()里的内容即username $str = preg_replace($preg,$replace, $info); //在$info里用$preg 匹配出的内容用$replace 替换掉上面替换成了<?php echo $this->fields['username']; ?> // //1表示获取()里的第一个分组内容 // echo $str;替换的内容是字符串不能当代码使用这时需要存入一个文件在加载进来就可以当代吗使用了; // 3.将新替换的内容存储到新文件中 file_put_contents($this->compile_dir.$tpl, $str); // 将生成的静态文件缓存下来 ob_start(); // 4.再将编译文件引入进来 include $this->compile_dir.$tpl; // 获取缓存内容 $a = ob_get_contents(); // 将缓存的数据存入cache文件中 file_put_contents($this->cache_dir.$tpl, $a); // 关闭缓存 ob_end_flush(); } } ``` 1.php ``` // 1.引入模板引擎 include 'Engine.class.php'; // 2.实例化模板引擎 $eng = new Engine; // 3.分配变量 $eng->assign('username','zhangsan'); // 4.编译模板并输出 $eng->display('1.html'); ``` ThinkPHP/Extend/Vendor/Smarty ThinkPHP\Library\Vendor\Smarty templates/1.html //模板文件 ``` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h3>{$username}</h3> </body> </html> ``` compile/1.html 编译文件 ``` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h3><?php echo $this->fields['username']; ?></h3> </body> </html> ``` cache/1.html //缓存文件 ``` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h3>zhangsan</h3> </body> </html> ``` preg_replace用法: preg_replace — 执行一个正则表达式的搜索和替换preg_replace (要搜索的模式。可以使一个字符串或字符串数组, 用于替换的字符串或字符串数组, 要进行搜索和替换的字符串或字符串数组) ``` <?php $patterns = array ('/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/', '/^\s*{(\w+)}\s*=/'); $replace = array ('\3/\4/\1\2', '$\1 ='); echo preg_replace($patterns, $replace, '{startDate} = 1999-5-27'); ``` 输出:$startDate = 5/27/1999 preg_replace ($pattern ,$replacement ,$subject):用replacement替换掉 $pattern 匹配出来的$subject里的内容 如果$replacement是字符串 $pattern是数组那么所有的模式都使用replacement这个字符串进行替换 如果pattern和replacement 都是数组,每个pattern使用replacement中对应的 元素进行替换。如果replacement中的元素比pattern中的少, 多出来的pattern使用空字符串进行替换