成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

PHP正則表達式

Nekron / 1114人閱讀

摘要:但在某些情況下,用正則表達式去驗證一個字符串比較復(fù)雜和費時。驗證信用卡號驗證域名從特定中提取域名將文中關(guān)鍵詞高亮顯示對于開發(fā)人員來說,正則表達式是一個非常有用的功能,它提供了查找,匹配,替換句子,單詞,或者其他格式的字符串。

正則表達式是程序開發(fā)中一個重要的元素,它提供用來描述或匹配文本的字符串,如特定的字符、詞或算式等。但在某些情況下,用正則表達式去驗證一個字符串比較復(fù)雜和費時。本文為你介紹10種常見的實用PHP正則表達式的寫法,希望對你的工作有所幫助

驗證E-mail地址

這是一個用于驗證電子郵件的正則表達式。但它并不是高效、完美的解決方案。在此不推薦使用。

$email = "[email protected]";
if
(preg_match("/^1[a-zA-Z0-9_]+(.+)@+(.+).{2,4}$/",$email))
{
echo "Your email is ok.";
} else {
echo "Wrong email address format";
}

為了更加有效驗證電子郵件地址,推薦使用filer_var。

if (filter_var("test+email@ansoncheung", FILTER_VALIDATE_EMAIL)) {
echo "Your email is ok.";
} else {
echo "Wrong email address format.";
}

驗證用戶名

這是一個用于驗證用戶名的實例,其中包括字母、數(shù)字(A-Z,a-z,0-9)、下劃線以及最低5個字符,最大20個字符。同時,也可以根據(jù)需要,對最小值和最大值做合理的修改。

$username = "user_name12";
if (preg_match("/^[a-zd_]{5,20}$/i", $username)) {
echo "Your username is ok.";
} else {
echo "Wrong username format.";
}

驗證電話號碼

這是一個驗證美國電話號碼的實例。

$phone = "(021)423-2323";
if (preg_match("/(?d{3})?[-s.]?d{3}[-s.]d{4}/x", $phone)) {
echo "Your phone number is ok.";
} else {
echo "Wrong phone number.";
}

驗證IP地址

這是一個用來驗證IPv4地址的實例。
$IP = "198.168.1.78";
if
(preg_match("/^(([1-9]?[0-9]|1[0-9]{2}|20-4|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|20-4|25[0-5])$/",$IP))
{
echo "Your IP address is ok.";
} else {
echo "Wrong IP address.";
}

驗證郵政編碼

這是一個用來驗證郵政編碼的實例。

$zipcode = "12345-5434";
if (preg_match("/^([0-9]{5})(-[0-9]{4})?$/i",$zipcode)) {
echo "Your Zip code is ok.";
} else {
echo "Wrong Zip code.";
}

驗證SSN(社會保險號)

這是一個驗證美國SSN的實例。

$ssn = "333-23-2329";
if (preg_match("/^[d]{3}-[d]{2}-[d]{4}$/",$ssn)) {
echo "Your SSN is ok.";
} else {
echo "Wrong SSN.";
}

驗證信用卡號

$cc = "378282246310005";
if
(preg_match("/^(?:4[0-9]{12}(?:[0-9]{3})?|51-5{14}|6011[0-9]{12}|3(?:0[0-5]|68)[0-9]{11}|347{13})$/",
$cc)) {
echo "Your credit card number is ok.";
} else {
echo "Wrong credit card number.";
}

驗證域名

$url = "http://ansoncheung.tk/";
if
(preg_match("/^(http|https|ftp)://(A-Z0-9(?:.A-Z0-9)+):?(d+)?/?/i",
$url)) {
echo "Your url is ok.";
} else {
echo "Wrong url.";
}

從特定URL中提取域名

$url = "http://ansoncheung.tk/articles";
preg_match("@^(?:http://)?(2+)@i", $url, $matches);
$host = $matches[1];
echo $host;

將文中關(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;

對于開發(fā)人員來說,正則表達式是一個非常有用的功能,它提供了 查找,匹配,替換 句子,單詞,或者其他格式的字符串。這篇文章主要介紹了15個超實用的php正則表達式,需要的朋友可以參考下。在這篇文章里,我已經(jīng)編寫了15個超有用的正則表達式,WEB開發(fā)人員都應(yīng)該將它收藏到自己的工具包。

驗證域名檢驗一個字符串是否是個有效域名

$url = "http://komunitasweb.com/";
if (preg_match("/^(http|https|ftp)://(A-Z0-9(?:.A-Z0-9)+):?(d+)?/?/i", $url)) {
echo "Your url is ok.";
} else {
echo "Wrong url.";
}

從一個字符串中 突出某個單詞

這是一個非常有用的在一個字符串中匹配出某個單詞 并且突出它,非常有效的搜索結(jié)果

$text = "Sample sentence from KomunitasWeb, regex 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;

突出查詢結(jié)果在你的 WordPress 博客里就像剛才我說的,上面的那段代碼可以很方便的搜索出結(jié)果,而這里是一個更好的方式去執(zhí)行搜索在某個WordPress的博客上打開你的文件 search.php ,然后找到 方法 the_title() 然后用下面代碼替換掉它

echo $title;

Now, just before the modified line, add this code:

$title = get_the_title();
$keys= explode(" ",$s);
$title = preg_replace("/(".implode("|", $keys) .")/iu",

"