php 慣用驗(yàn)證類及正則
來源:易賢網(wǎng) 閱讀:755 次 日期:2014-09-18 10:22:30
溫馨提示:易賢網(wǎng)小編為您整理了“php 慣用驗(yàn)證類及正則”,方便廣大網(wǎng)友查閱!

php 常用驗(yàn)證類及正則

正則表達(dá)式在遇到新的時(shí)候?qū)?huì)不斷更新

include "ValidateParameterConfig.php";

class Validation

{

private static function getRexp($rexp)

{

$_rexp = array (

'letter_number'=>'/^[0-9A-Za-z]+$/',//只有字母數(shù)字包括大小寫

'account'=>'/^[0-9A-Za-z_]+$/',//只有字母數(shù)字下劃線包括大小寫

'ids'=>'/^[0-9]+(,[0-9]+)*$/',//驗(yàn)證多個(gè)id以','分割的類型 例如'1,2,3,4,5'

'number'=>'/^[0-9]+$/',//只可以使數(shù)字

'personal_card'=>'/^[0-9A-Za-z]+$/',//身份證

'email'=>'/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/',//郵箱

'date'=>'/^((((19|20)d{2})-(0?(1|[3-9])|1[012])-(0?[1-9]|[12]d|30))|(((19|20)d{2})-(0?[13578]|1[02])-31)|(((19|20)d{2})-0?2-(0?[1-9]|1d|2[0-8]))|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))-0?2-29))$/'//日期,包含了閏年

);

if (isset($_rexp[$rexp])) {

return $_rexp[$rexp];

} else {

return $rexp_not_defind;

}

}

public static function validate($data, $config)

{

$_config = ValidateParameterConfig::getConfig($config);

$k = self::allowExist($data, $_config);

if ($k !== null) {

return $k;

}

foreach($_config as $k=>$c) {

if (isset($data[$k]))

{

if(isset($c['rexp']))

{

if (self::vRexp( $data[$k], $c['rexp']) === false) {

return $k;

}

}

if (isset($c['length']))

{

if (self::vLength($data[$k], $c['length']))

{

return $k;

}

}

if (isset($c['min_length']))

{

if (!self::vMinLength($data[$k], $c['min_length']))

{

return $k;

}

}

if (isset($c['max_length']))

{

if (!self::vMaxLength($data[$k], $c['max_length']))

{

return $k;

}

}

}

}

return null;

}

private static function allowExist($data, $config)

{

foreach ($config as $k=>$v)

{

if (!isset($v['allow_exist']) || $v['allow_exist'] == true) {

if (!isset($data[$k]))

{

return $k;

}

}

}

return null;

}

public static function vRexp($data, $rexp) {

$_rexp = self::getRexp($rexp);

if (preg_match($_rexp, $data) == false) {

return false;

}

return true;

}

public static function vLength($data, $l) {

if (strlen(trim($data)) == $l) {

return false;

}

return true;

}

public static function vMinLength($data, $l) {

if (strlen(trim($data)) < $l) {

return false;

}

return true;

}

public static function vMaxLength($data, $l) {

if (strlen(trim($data)) > $l) {

return false;

}

return true;

}

public static function vLetterNumber($data) {

if (preg_match(self::getRexp('letter_number'), $data) == false) {

return false;

}

return true;

}

public static function vLetterNumber_($data) {

if (preg_match(self::getRexp('letter_number_'), $data) == false) {

return false;

}

return true;

}

public static function vNumber($data) {

if (preg_match(self::getRexp('number'), $data) == false) {

return false;

}

return true;

}

public static function vEmail($data) {

if (preg_match(self::getRexp('email'), $data) == false) {

return false;

}

return true;

}

class ValidateParameterConfig

{

public static function getConfig ($key)

{

//'allow_exist'=>true或allow_exist不存在表示該參數(shù)必傳

$_config = array(

'test'=>array(

'letter_number'=>array('rexp'=>'letter_number', 'allow_exist'=>true),

'number'=>array('rexp'=>'number', 'min_length'=>1'allow_exist'=>false),

'account'=>array('rexp'=>'account', 'max_length'=>20),

)

);

if (isset($_config[$key])) {

return $_config[$key];

} else {

return $config_not_defind;

}

}

}

使用例子:

從應(yīng)用端發(fā)過來的參數(shù)為

$arr = array('letter_number'=>'abc123', 'account'=>'acb1234_');

//參數(shù)驗(yàn)證

$_msg = Validation::validate($arr, 'test');

if ($_msg !== null) {

return $_msg . ' is invalid parameter';

}

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:php 慣用驗(yàn)證類及正則
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)