发布时间:2015-05-27 19:17:55作者:知识屋
1、验证E-mail
用filer_var 比用正则匹配更加好
if (filter_var('test+email@ansoncheung', FILTER_VALIDATE_EMAIL)) { echo "Your email is ok.";} else { echo "Wrong email address format.";}
2、验证用户名(验证用户名5-20之间)
$username = "user_name12";if (preg_match('/^[a-z/d_]{5,20}$/i', $username)) { echo "Your username is ok.";} else { echo "Wrong username format.";}
3、验证ip地址
$IP = "198.168.1.78";if (preg_match('/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/',$IP)) { echo "Your IP address is ok.";} else { echo "Wrong IP address.";}
4、验证邮编
$zipcode = "12345-5434"; if (preg_match("/^([0-9]{5})(-[0-9]{4})?$/i",$zipcode)) { echo "Your Zip code is ok."; } else { echo "Wrong Zip code."; }
5、验证域名
$url = "http://ansoncheung.tk/"; if (preg_match('/^(http|https|ftp):////([A-Z0-9][A-Z0-9_-]*(?:/.[A-Z0-9][A-Z0-9_-]*)+):?(/d+)?//?/i', $url)) { echo "Your url is ok."; } else { echo "Wrong url."; }
$url = "http://ansoncheung.tk/articles"; preg_match('@^(?:http://)?([^/]+)@i', $url, $matches); $host = $matches[1];echo $host;
$text = "Sample sentence from AnsonCheung.tk, regular expression has become popular in web programming. Now we learn regex. According to wikipedia, Regular expressions (abbreviated as regex or regexp, with plural forms regexes, regexps, or regexen) are written in a formal language that can be interpreted by a regular expression processor";$text = preg_replace("//b(regex)/b/i", '/1', $text);echo $text;
如何对PHP文件进行加密方法 PHP实现加密的几种方式介绍
php生成圆角图片的方法 电脑中php怎么生成圆角图片教程
用PHP构建一个留言本方法步骤 php怎么实现留言板功能
php中三元运算符用法 php中的三元运算符使用说明
php文件如何怎么打开方式介绍 php文件用什么打开方法
PHP怎么插入数据库方法步骤 php编程怎么导入数据库教程
如何安装PHPstorm并配置方法教程 phpstorm安装后要进行哪些配置
PHP 获取远程文件大小的3种解决方法 如何用PHP获取远程大文件的大小
20个实用PHP实例代码 php接口开发实例代码详细介绍
如何架设PHP服务器方法步骤 怎么搭建php服务器简单教程