PHP常用正則表達(dá)式
header("Content-Type:text/html;charset=utf-8"),這一句一般都是用于設(shè)置頁面的字符集,防止出現(xiàn)亂碼,雖然跟本節(jié)沒多大關(guān)系,但也可以當(dāng)作基礎(chǔ)知識。
/pic/strong>
1
2
3
$preg = '/^(https?:/pic/i';
$str = 'www.liqingbo.cn';
echo preg_match($preg, $str);
/pic/strong>
1
2
3
$preg = '/^([a-z]+):/pic/i';
$str = '/pic/p>
echo preg_match($preg, $str);
/pic/strong>
1
2
3
$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);
/pic/strong>
1
2
3
4
$preg = '/^<([a-z]+)([^<]+)*(?:>(.*)</1>|s+/>)$/';
$str = '<a href="/pic/a>';
$res = preg_match_all($preg, $str, $matches);
var_dump($matches);
/pic/strong>
1
2
3
4
5
$preg = '/<img[^>]+(src="([^"<>']+)"|src='([^"<>']+)')[^<>]*>/';
$html = '<div><a href="/pic/pic/src/img0.gif" /><img src="/pic/src/img1.gif" /></a></div>';
$res = preg_match_all($preg, $html, $matches, PREG_PATTERN_ORDER);
/pic/p>
echo $matches[2][0]; /pic/p>
/pic/strong>
1
2
3
$preg = '/^([a-z0-9_.-]+)@([a-z0-9.-]+).([a-z]+)$/i';
$str = 'jeddy_liu-jin@gmail.com';
echo preg_match($preg, $str);
/pic/strong>
1
2
3
$preg = '/^[a-z0-9@_.-]{6,18}$/';
$str = 'liujin@1234.com';
echo preg_match($preg, $str);
/pic/strong>
1
2
3
$preg = '/^[a-z0-9_-]{3,16}$/';
$str = 'liujin-88';
echo preg_match($preg, $str);
/pic/strong>
1
2
3
$preg = '/^(0d{2,3})-?(d{7,8})$/';
$str = '015-5415488';
echo preg_match($preg, $str);
/pic/strong>
1
2
3
$preg = '/^1[3|4|5|8]d{9}$/';
$str = '18012345678';
echo preg_match($preg, $str);
/pic/strong>
1
2
3
$preg = '/^[1-9]d{5}$/';
$str = '415000';
echo preg_match($preg, $str);
/pic/strong>
1
2
3
$preg = '/(^d{15}$)|(^d{18}$)/';
$str = '430701198806520';
echo preg_match($preg, $str);
/pic/strong>
1
2
3
4
$preg = '/^[x{4e00}-x{9fa5}]+$/u';
$str = 'PHP博客';
preg_match($preg, $str, $match);
var_dump($match);
【PHP常用正則表達(dá)式】相關(guān)文章:
PHP常用的正則表達(dá)式11-06
PHP常用的正則表達(dá)式是什么02-26
用php常用表單驗證的正則表達(dá)式02-27
php漢字正則表達(dá)式12-08