Original 10 useful PHP regular Expression summary
A regular expression is an important element in the development of a program that provides a string that describes or matches text, such as a specific character, word, or calculation. But in some cases, using regular expressions to verify that a string is more complex and time-consuming. This article gives you a description of 10 common PHP regular expressions that I hope will help you with your work. 1. Verify that E-mail address This is a regular expression that is used to validate e-mail messages. But it's not an efficient, perfect solution. Not recommended for use here. $email= "[Email protected]"; if (Preg_match('/^[^0-9][a-za-z0-9_]+ ([.] [a-za-z0-9_]+) *[@][a-za-z0-9_]+ ([.] [a-za-z0-9_]+) *[.] [A-za-z] {2,4}$/',$email)) { Echo"Your email is OK."; } Else { Echo"wrong email address format"; For more effective verification of e-mail addresses, we recommend using Filer_var. if(Filter_var (' [email protected] ',filter_validate_email)) { Echo"Your email is OK."; } Else { Echo"Wrong email address format."; }2.Verify user Name This is an instance that validates the user name, including letters, numbers (a-z,a-z,0-9), underscore, and a minimum of 5 characters, up to a maximum of 20 characters. At the same time, the minimum and maximum values can be modified as needed. $username= "User_name12"; if(Preg_match('/^[a-z\d_]{5,20}$/i ',$username)) { Echo"Your username is ok."; } Else { Echo"wrong username format."; }3.Verify the phone number This is an instance of verifying a U.S. phone number. $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."; }4.Verify IP address This is an instance that is used to verify the IPV4 address. $IP= "198.168.1.78"; if (Preg_match('/^ ([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]) $/',$IP)) { Echo"Your IP address is OK."; } Else { Echo"wrong IP address."; }5.Verify the ZIP code this is an instance to validate the ZIP code. $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."; }6.Verify SSN (Social Security number) This is an example of verifying a US ssn. $SSN= "333-23-2329"; if(Preg_match('/^[\d]{3}-[\d]{2}-[\d]{4}$/',$SSN)) { Echo"Your SSN is OK."; } Else { Echo"Wrong SSN."; }7.Verify credit card number$CC= "378282246310005"; if (Preg_match('/^ (?: 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}) $/',$CC)) { Echo"Your credit card number is OK."; } Else { Echo"Wrong credit card number."; }8.Verify the domain name$url= "http://www.jakehu.me/"; if (Preg_match('/^ (http|https|ftp): \/\/([a-z0-9][a-z0-9_-]* (?: \. [A-z0-9] [a-z0-9_-]*) +):? (\d+)? \/?/i ',$url)) { Echo"Your URL is ok."; } Else { Echo"Wrong URL."; }9.extracting a domain name from a specific URL$url= "Http://www.jakehu.me/articles"; Preg_match(' @^ (?:/HTTP//)? ([^/]+) @i ',$url,$matches); $host=$matches[1]; Echo $host;10.highlight the keywords in the text$text= "Sample sentence from ansoncheung.tk, regular expression had 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) is written in a formal language so can be interpreted by a regular expression processor"; $text=Preg_replace("/\b (regex) \b/i", ' \1 ',$text); Echo $text;
10 useful php Regular expression summaries