代碼如下 |
複製代碼 |
class validation{ function check_email($value){ //check a email address return (bool) preg_match('/^[a-z0-9._%+-]+@(?:[a-z0-9-]+.)+[a-z]{2,4}$/i', $value); } function check_ip($value){ //check a ip address return (bool) preg_match("/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){4}$/", $value."."); } function check_date($date, $format="dd/mm/yy"){ //check a date if(!preg_match("/([0-9]+)([./-])([0-9]+)(\2)([0-9]+)/", $date, $m)) return false; $f = explode("/", $format); $d[$f[0]] = $m[1]; $d[$f[1]] = $m[3]; $d[$f[2]] = $m[5]; return checkdate($d['mm'], $d['dd'], $d['yyyy'].$d['yy']); } function check_url($url){ //check a url return (bool) preg_match("/^(?:(?:ht|f)tp(?:s?)://|~/|/)?(?:(?:w+.)+)w+(?::d+)?(?:(?:/[^/?#]+)+)?/?(?:?[^?]*)?(#.*)?$/i", $url); } } |
//調用方法
代碼如下 |
複製代碼 |
$validation = new validation(); //檢測郵箱 var_dump($validation->check_email("abc@abc.abc.com"));//true var_dump($validation->check_email("abc.abc@abc.com"));//false |
//ip檢測
代碼如下 |
複製代碼 |
var_dump($validation->check_ip("127.0.0.1.0")); var_dump($validation->check_ip("256.0.0.1")); |
//日期格式檢測
代碼如下 |
複製代碼 |
//true var_dump($validation->check_date("2010/1/20", "yyyy/mm/dd")); var_dump($validation->check_date("1/20/10", "mm/dd/yy")); var_dump($validation->check_date("2010-20-1", "yyyy/dd/mm")); //false var_dump($validation->check_date("2010/2/29", "yyyy/mm/dd")); var_dump($validation->check_date("2010/1/20", "yyyy/dd/mm")); var_dump($validation->check_date("2010-20/1", "yyyy/dd/mm")); |
/*
此類的使用方法非常簡單吧,只要建立類就可以輕鬆的驗證郵箱,日期,地址,ip是否為合法的的哦。
*/