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培训学院
首发:http://php.itheima.com/