10个实用的PHP正则表达式汇总
1. 验证E-mail地址
这是一个用于验证电子邮件的正则表达式。但它并不是高效、完美的解决方案。在此不推荐使用。
2. 验证用户名
这是一个用于验证用户名的实例,其中包括字母、数字(A-Z,a-z,0-9)、下划线以及最低5个字符,最大20个字符。同时,也可以根据需要,对最小值和最大值做合理的修改。
3. 验证电话号码
这是一个验证美国电话号码的实例。
这是一个用来验证IPv4地址的实例。
5. 验证邮政编码
这是一个用来验证邮政编码的实例。
这是一个验证美国SSN的实例。
这是一个用于验证电子邮件的正则表达式。但它并不是高效、完美的解决方案。在此不推荐使用。
<span class="pln">$email </span><span class="pun">=</span><span class="pln"> </span><span class="str">"test@ansoncheung.tk"</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">preg_match</span><span class="pun">(</span><span class="str">'/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/'</span><span class="pun">,</span><span class="pln">$email</span><span class="pun">))</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> echo </span><span class="str">"Your email is ok."</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> echo </span><span class="str">"Wrong email address format"</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span>为了更加有效验证电子邮件地址,推荐使用filer_var。
<span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">filter_var</span><span class="pun">(</span><span class="str">'test+email@ansoncheung'</span><span class="pun">,</span><span class="pln"> FILTER_VALIDATE_EMAIL</span><span class="pun">))</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> echo </span><span class="str">"Your email is ok."</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> echo </span><span class="str">"Wrong email address format."</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span>
2. 验证用户名
这是一个用于验证用户名的实例,其中包括字母、数字(A-Z,a-z,0-9)、下划线以及最低5个字符,最大20个字符。同时,也可以根据需要,对最小值和最大值做合理的修改。
<span class="pln">$username </span><span class="pun">=</span><span class="pln"> </span><span class="str">"user_name12"</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">preg_match</span><span class="pun">(</span><span class="str">'/^[a-z\d_]{5,20}$/i'</span><span class="pun">,</span><span class="pln"> $username</span><span class="pun">))</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> echo </span><span class="str">"Your username is ok."</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> echo </span><span class="str">"Wrong username format."</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span>
3. 验证电话号码
这是一个验证美国电话号码的实例。
<span class="pln">$phone </span><span class="pun">=</span><span class="pln"> </span><span class="str">"(021)423-2323"</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">preg_match</span><span class="pun">(</span><span class="str">'/\(?\d{3}\)?[-\s.]?\d{3}[-\s.]\d{4}/x'</span><span class="pun">,</span><span class="pln"> $phone</span><span class="pun">))</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> echo </span><span class="str">"Your phone number is ok."</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> echo </span><span class="str">"Wrong phone number."</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span>4. 验证IP地址
这是一个用来验证IPv4地址的实例。
<span class="pln">$IP </span><span class="pun">=</span><span class="pln"> </span><span class="str">"198.168.1.78"</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">preg_match</span><span class="pun">(</span><span class="str">'/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/'</span><span class="pun">,</span><span class="pln">$IP</span><span class="pun">))</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> echo </span><span class="str">"Your IP address is ok."</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> echo </span><span class="str">"Wrong IP address."</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span>
5. 验证邮政编码
这是一个用来验证邮政编码的实例。
<span class="pln">$zipcode </span><span class="pun">=</span><span class="pln"> </span><span class="str">"12345-5434"</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">preg_match</span><span class="pun">(</span><span class="str">"/^([0-9]{5})(-[0-9]{4})?$/i"</span><span class="pun">,</span><span class="pln">$zipcode</span><span class="pun">))</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> echo </span><span class="str">"Your Zip code is ok."</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> echo </span><span class="str">"Wrong Zip code."</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span>6. 验证SSN(社会保险号)
这是一个验证美国SSN的实例。
<span class="pln">$ssn </span><span class="pun">=</span><span class="pln"> </span><span class="str">"333-23-2329"</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">preg_match</span><span class="pun">(</span><span class="str">'/^[\d]{3}-[\d]{2}-[\d]{4}$/'</span><span class="pun">,</span><span class="pln">$ssn</span><span class="pun">))</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> echo </span><span class="str">"Your SSN is ok."</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> echo </span><span class="str">"Wrong SSN."</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span>7. 验证信用卡号
<span class="pln">$cc </span><span class="pun">=</span><span class="pln"> </span><span class="str">"378282246310005"</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">preg_match</span><span class="pun">(</span><span class="str">'/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/'</span><span class="pun">,</span><span class="pln"> $cc</span><span class="pun">))</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> echo </span><span class="str">"Your credit card number is ok."</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> echo </span><span class="str">"Wrong credit card number."</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span>8. 验证域名
<span class="pln">$url </span><span class="pun">=</span><span class="pln"> </span><span class="str">"http://ansoncheung.tk/"</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">preg_match</span><span class="pun">(</span><span class="str">'/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i'</span><span class="pun">,</span><span class="pln"> $url</span><span class="pun">))</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> echo </span><span class="str">"Your url is ok."</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> echo </span><span class="str">"Wrong url."</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span>9. 从特定URL中提取域名
<span class="pln">$url </span><span class="pun">=</span><span class="pln"> </span><span class="str">"http://ansoncheung.tk/articles"</span><span class="pun">;</span><span class="pln"> preg_match</span><span class="pun">(</span><span class="str">'@^(?:http://)?([^/]+)@i'</span><span class="pun">,</span><span class="pln"> $url</span><span class="pun">,</span><span class="pln"> $matches</span><span class="pun">);</span><span class="pln"> $host </span><span class="pun">=</span><span class="pln"> $matches</span><span class="pun">[</span><span class="lit">1</span><span class="pun">];</span><span class="pln"> echo $host</span><span class="pun">;</span>10. 将文中关键词高亮显示
<span class="pln">$text </span><span class="pun">=</span><span class="pln"> </span><span class="str">"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"</span><span class="pun">;</span><span class="pln"> $text </span><span class="pun">=</span><span class="pln"> preg_replace</span><span class="pun">(</span><span class="str">"/\b(regex)\b/i"</span><span class="pun">,</span><span class="pln"> </span><span class="str">'<span style="background:#5fc9f6">\1</span>'</span><span class="pun">,</span><span class="pln"> $text</span><span class="pun">);</span><span class="pln"> echo $text</span><span class="pun">;</span>