六、实战——注册表单验证
1.前台页面准备,html
<form action="b.php" method="post">
用户名<input type="text" name="username" id="username" value="" />
<br><br>
email<input type="text" name="email" id="email" value="" />
<br><br>
<input type="submit" value="注册">
</form>
2.注册表单验证,php
require_once 'a.php';#引入文件
$regex = new regexTool();
if (!$regex->noEmpty($_POST['username']))
exit('用户名不能为空');
if (!$regex->isEmail($_POST['email']))
exit('email格式错误');
七、实战——仿Smarty简易模板引擎
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/12/17 0017
* Time: 上午 11:36
*/
class template{
#1.模板引擎类-成员属性
private $templateDir;#存储模板引擎源文件的所在目录
private $compileDir;#存储编译之后文件的存放目录
private $leftTag = '{#';#模板需要替换的变量,做标记
private $rightTag = '#}';
private $currentTemp = '';#存储当前正在编译的模板文件
private $outputHtml;#存放当前正在编译中的html代码
private $varPool = array();#把模板中需要用到的变量放到改模板中使用
#2.模板引擎类-构造函数
public function__construct($templateDir,$compileDir,$leftTag=null,$rightTag=null){
$this->templateDir = $templateDir;
$this->compileDir = $compileDir;
if (!empty($leftTag)) $this->leftTag = $leftTag;
if (!empty($rightTag)) $this->rightTag = $rightTag;
}
#3. 模板引擎类-写入和获取数据
public function assign($tag,$var){
$this->varPool[$tag] = $var;#把模板中需要的变量都放到这
}
/**
* 获取数据
* @param $tag
* @return array
*/
public function getVar($tag){
return $this->varPool[$tag];
}
#4. 模板引擎类-获取模板源文件
/**
* 获取模板的源文件
*/
public function getSourceTemplate($templateName,$ext = '.html'){
$this->currentTemp = $templateName;
$sourceFilename = $this->templateDir.$this->currentTemp.$ext;
$this->outputHtml = file_get_contents($sourceFilename);
}
#5. 模板引擎类-模板编译
/**
* 对源文件进行编译
*/
public function compileTemplate($templateName = null,$ext = '.html'){
$templateName = empty($templateName) ? $this->currentTemp : $templateName;
//核心代码,正则替换
$pattern = '/'.preg_quote($this->leftTag);
$pattern .= ' *\$([a-zA-Z_]\w*) *';#满足php变量的命名规则
$pattern .= preg_quote($this->rightTag).'/';
$this->outputHtml = preg_replace($pattern,'<?php echo $this->getVar(\'$1\'); ?>',$this->outputHtml);
$compileFilename = $this->compileDir.md5($templateName).$ext;
file_put_contents($compileFilename,$this->outputHtml);
}
#6.模板引擎类-显示模板
public function display($templateName = null, $ext = '.html'){
$templateName = empty($templateName) ? $this->currentTemp : $templateName;
include_once $this->compileDir.md5($templateName).$ext;
}
}
7.正则替换测试
$pattern = '/\{#';
$pattern .= ' *\$([a-zA-Z_]\w*) *';#满足php变量的命名规则
$pattern .= '#\}/';
$subject = 'aaa:{#$test#}';
$a = preg_replace($pattern,'<?php echo $$test ?> ',$subject);
echo $a;
8. 模板引擎测试
index.php文件代码
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/12/17 0017
* Time: 下午 2:18
*/
require_once 'template.php';
$baseDir = str_replace('\\','/',dirname(__FILE__));
$temp = new template($baseDir.'/source/',$baseDir.'/compiled/');
$temp->assign('title','厉害了1');
$temp->assign('test','我很厉害');
$temp->getSourceTemplate('index');
$temp->compileTemplate();
$temp->display();
index.html文件代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{#$title#}</title>
</head>
<body>
测试:{#$test#}
</body>
</html>