6. 驗證SSN(社會保險號)
這是一個驗證美國SSN的實例。
$ssn = "333232329";
if (preg_match('/^[\d]{3}[\d]{2}[\d]{4}$/',$ssn)) {
echo "Your SSN is ok.";
} else {
echo "Wrong SSN.";
}
7. 驗證信用卡號
$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. 驗證域名
$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. 將文中關鍵詞高亮顯示
$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培訓學院
首發(fā):http://php.itheima.com/