6. 驗(yàn)證SSN(社會(huì)保險(xiǎn)號)
這是一個(gè)驗(yàn)證美國SSN的實(shí)例。
$ssn = "333232329";
if (preg_match('/^[\d]{3}[\d]{2}[\d]{4}$/',$ssn)) {
echo "Your SSN is ok.";
} else {
echo "Wrong SSN.";
}
7. 驗(yàn)證信用卡號
$cc = "378282246310005";
if
(preg_match('/^(?:4[09]{12}(?:[09]{3})?|5[15][09]{14}|6011[09]{12}|3(?:0[05]|[68][09])[09]{11}|3[47][09]{13})$/',
$cc)) {
echo "Your credit card number is ok.";
} else {
echo "Wrong credit card number.";
}
8. 驗(yàn)證域名
$url = "http://ansoncheung.tk/";
if
(preg_match('/^(http|https|ftp):\/\/([AZ09][AZ09_](?:\.[AZ09][AZ09_])+):?(\d+)?\/?/i',
$url)) {
echo "Your url is ok.";
} else {
echo "Wrong url.";
}
9. 從特定URL中提取域名
4$url = "http://ansoncheung.tk/articles";
preg_match('@^(?:http://)?([^/]+)@i', $url, $matches);
$host = $matches[1];
echo $host;
10. 將文中關(guān)鍵詞高亮顯示
$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;
本文版權(quán)歸黑馬程序員PHP培訓(xùn)學(xué)院所有,歡迎轉(zhuǎn)載,轉(zhuǎn)載請注明作者出處,謝謝!作者:黑馬程序員PHP培訓(xùn)學(xué)院
首發(fā):http://php.itheima.com/