header("Content-Type:text/html;charset=utf-8"); 這一句一般都是用于設(shè)置頁面的字符集,防止出現(xiàn)亂碼,雖然跟本節(jié)沒多大關(guān)系,但也可以當(dāng)作基礎(chǔ)知識(shí)。
//匹配英文域名網(wǎng)址:http,https。域名中沒有下劃線,后綴為字母 $preg = '/^(https?:\/\/)?([a-z\d\.-]+)\.([a-z]+)$/i' ;
$str = 'www.' ;
echo preg_match( $preg , $str );
|
//匹配url $preg = '/^([a-z]+):\/\/([^\s]*)/i' ;
$str = 'http://blog.' ;
echo preg_match( $preg , $str );
|
//匹配IP地址 $preg = '/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/' ;
$str = '255.255.255.250' ;
echo preg_match( $preg , $str );
|
//匹配一個(gè)html標(biāo)簽 $preg = '/^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/' ;
$str = '<a href="http://www.">菜根網(wǎng)</a>' ;
$res = preg_match_all( $preg , $str , $matches );
var_dump( $matches );
|
//從一段html中提取一張圖片 //匹配電子郵箱
$preg = '/^([a-z0-9_\.-]+)@([a-z0-9\.-]+)\.([a-z]+)$/i' ;
$str = 'jeddy_liu-jin@gmail.com' ;
echo preg_match( $preg , $str );
|
//匹配密碼 $preg = '/^[a-z0-9@_\.-]{6,18}$/' ;
$str = 'liujin@1234.com' ;
echo preg_match( $preg , $str );
|
//匹配用戶名 $preg = '/^[a-z0-9_-]{3,16}$/' ;
$str = 'liujin-88' ;
echo preg_match( $preg , $str );
|
//國(guó)內(nèi)座機(jī) $preg = '/^(0\d{2,3})-?(\d{7,8})$/' ;
$str = '015-5415488' ;
echo preg_match( $preg , $str );
|
//國(guó)內(nèi)手機(jī) $preg = '/^1[3|4|5|8]\d{9}$/' ;
$str = '18012345678' ;
echo preg_match( $preg , $str );
|
//匹配郵編 $preg = '/^[1-9]\d{5}$/' ;
$str = '415000' ;
echo preg_match( $preg , $str );
|
//匹配身份證號(hào) $preg = '/(^\d{15}$)|(^\d{18}$)/' ;
$str = '430701198806520' ;
echo preg_match( $preg , $str );
|
//匹配漢字 $preg = '/^[\x{4e00}-\x{9fa5}]+$/u' ;
$str = 'PHP博客' ;
preg_match( $preg , $str , $match );
var_dump( $match );
|