php可扩展的验证类实例(可对邮件、手机号、URL等验证)_php技巧
本文实例讲述了php可扩展的验证类。分享给大家供大家参考。具体分析如下:
这里介绍一个可扩展的php验证类,
类里面可以的各类验证可自行调整实现,现在为基本实现方式。
需要添加规则的话, 直接定义方法,方法名即为规则名称。具体参考使用方法。
require_once('./Validator.class.php'); $data = array( 'nickname' => 'heno' , 'realname' => 'steven', 'age' => 25, 'mobile' => '1521060426'); $validator = new Validator($data); $validator->setRule('nickname', 'required'); $validator->setRule('realname', array('length' => array(1,6), 'required')); $validator->setRule('age', array('required', 'digit')); $validator->setRule('mobile', array('mobile')); $result = $validator->validate(); var_dump($result); var_dump($validator->getResultInfo());
Validator.class.php文件如下:
<?php /** * Validator 数据验证类 * @package library * @category library * @author Steven * @version 1.0 */ /** * Validator 数据验证类 * @package library * @category library * @author Steven * @version 1.0 */ class Validator { /** * 待校验数据 * @var array */ private $_data; /** * 校验规则 * @var array */ private $_ruleList = null; /** * 校验结果 * @var bool */ private $_result = null; /** * 校验数据信息 * @var array */ private $_resultInfo = array(); /** * 构造函数 * @param array $data 待校验数据 */ public function __construct($data = null) { if ($data) { $this->_data = $data; } } /** * 设置校验规则 * @param string $var 带校验项key * @param mixed $rule 校验规则 * @return void */ public function setRule($var, $rule) { $this->_ruleList[$var] = $rule; } /** * 检验数据 * @param array $data * <code> * $data = array('nickname' => 'heno' , 'realname' => 'steven', 'age' => 25); * $validator = new Validator($data); * $validator->setRule('nickname', 'required'); * $validator->setRule('realname', array('lenght' => array(1,4), 'required')); * $validator->setRule('age', array('required', 'digit')); * $result = $validator->validate(); * var_dump($validator->getResultInfo()); * </code> * @return bool */ public function validate($data = null) { $result = true; /* 如果没有设置校验规则直接返回 true */ if ($this->_ruleList === null || !count($this->_ruleList)) { return $result; } /* 已经设置规则,则对规则逐条进行校验 */ foreach ($this->_ruleList as $ruleKey => $ruleItem) { /* 如果检验规则为单条规则 */ if (!is_array($ruleItem)) { $ruleItem = trim($ruleItem); if (method_exists($this, $ruleItem)) { /* 校验数据,保存校验结果 */ $tmpResult = $this->$ruleItem($ruleKey); if (!$tmpResult) { $this->_resultInfo[$ruleKey][$ruleItem] = $tmpResult; $result = false; } } continue; } /* 校验规则为多条 */ foreach ($ruleItem as $ruleItemKey => $rule) { if (!is_array($rule)) { $rule = trim($rule); if (method_exists($this, $rule)) { /* 校验数据,设置结果集 */ $tmpResult = $this->$rule($ruleKey); if (!$tmpResult) { $this->_resultInfo[$ruleKey][$rule] = $tmpResult; $result = false; } } } else { if (method_exists($this, $ruleItemKey)) { /* 校验数据,设置结果集 */ $tmpResult = $this->$ruleItemKey($ruleKey, $rule); if (!$tmpResult) { $this->_resultInfo[$ruleKey][$ruleItemKey] = $tmpResult; $result = false; } } } } } return $result; } /** * 获取校验结果数据 * @return [type] [description] */ public function getResultInfo() { return $this->_resultInfo; } /** * 校验必填参数 * @param string $varName 校验项 * @return bool */ public function required($varName) { $result = false; if (is_array($this->_data) && isset($this->_data[$varName])) { $result = true; } return $result; } /** * 校验参数长度 * * @param string $varName 校验项 * @param array $lengthData array($minLen, $maxLen) * @return bool */ public function length($varName, $lengthData) { $result = true; /* 如果该项没有设置,默认为校验通过 */ if ($this->required($varName)) { $varLen = mb_strlen($this->_data[$varName]); $minLen = $lengthData[0]; $maxLen = $lengthData[1]; if ($varLen < $minLen || $varLen > $maxLen) { $result = true; } } return $result; } /** * 校验邮件 * @param string $varName 校验项 * @return bool */ public function email($varName) { $result = true; /* 如果该项没有设置,默认为校验通过 */ if ($this->required($varName)) { $email = trim($this->_data[$varName]); if (preg_match('/^[-\w]+?@[-\w.]+?$/', $email)) { $result = false; } } return $result; } /** * 校验手机 * @param string $varName 校验项 * @return bool */ public function mobile($varName) { $result = true; /* 如果该项没有设置,默认为校验通过 */ if ($this->required($varName)) { $mobile = trim($this->_data[$varName]); if (!preg_match('/^1[3458]\d{10}$/', $mobile)) { $result = false; } } return $result; } /** * 校验参数为数字 * @param string $varName 校验项 * @return bool */ public function digit($varName) { $result = false; if ($this->required($varName) && is_numeric($this->_data[$varName])) { $result = true; } return $result; } /** * 校验参数为身份证 * @param string $varName 校验项 * @return bool */ public function ID($ID) { } /** * 校验参数为URL * @param string $varName 校验项 * @return bool */ public function url($url) { $result = true; /* 如果该项没有设置,默认为校验通过 */ if ($this->required($varName)) { $url = trim($this->_data[$varName]); if(!preg_match('/^(http[s]?::)?\w+?(\.\w+?)$/', $url)) { $result = false; } } return $result; } } ?>
希望本文所述对大家的php程序设计有所帮助。

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PHP仍然流行的原因是其易用性、靈活性和強大的生態系統。 1)易用性和簡單語法使其成為初學者的首選。 2)與web開發緊密結合,處理HTTP請求和數據庫交互出色。 3)龐大的生態系統提供了豐富的工具和庫。 4)活躍的社區和開源性質使其適應新需求和技術趨勢。

多次調用session_start()會導致警告信息和可能的數據覆蓋。 1)PHP會發出警告,提示session已啟動。 2)可能導致session數據意外覆蓋。 3)使用session_status()檢查session狀態,避免重複調用。

IIS和PHP可以兼容,通過FastCGI實現。 1.IIS通過配置文件將.php文件請求轉發給FastCGI模塊。 2.FastCGI模塊啟動PHP進程處理請求,提高性能和穩定性。 3.實際應用中需注意配置細節、錯誤調試和性能優化。

session_start()iscucialinphpformanagingusersessions.1)ItInitiateSanewsessionifnoneexists,2)resumesanexistingsessions,and3)setsasesessionCookieforContinuityActinuityAccontinuityAcconActInityAcconActInityAcconAccRequests,EnablingApplicationsApplicationsLikeUseAppericationLikeUseAthenticationalticationaltication and PersersonalizedContentent。

AI可以幫助優化Composer的使用,具體方法包括:1.依賴管理優化:AI分析依賴關係,建議最佳版本組合,減少衝突。 2.自動化代碼生成:AI生成符合最佳實踐的composer.json文件。 3.代碼質量提升:AI檢測潛在問題,提供優化建議,提高代碼質量。這些方法通過機器學習和自然語言處理技術實現,幫助開發者提高效率和代碼質量。

Laravel優化Web開發流程的方法包括:1.使用路由系統管理URL結構;2.利用Blade模板引擎簡化視圖開發;3.通過隊列處理耗時任務;4.使用EloquentORM簡化數據庫操作;5.遵循最佳實踐提高代碼質量和可維護性。

在IIS上配置和運行PHP需要以下步驟:1)下載並安裝PHP,2)配置IIS並添加FastCGI模塊,3)創建並設置應用池,4)創建網站並綁定到應用池。通過這些步驟,你可以在Windows服務器上輕鬆部署PHP應用,並通過配置擴展和優化性能來提升應用的穩定性和效率。

HTML5帶來了五個關鍵改進:1.語義化標籤提升了代碼清晰度和SEO效果;2.多媒體支持簡化了視頻和音頻嵌入;3.表單增強簡化了驗證;4.離線與本地存儲提高了用戶體驗;5.畫布與圖形功能增強了網頁的可視化效果。
