10 practical PHP regular expression summary, PHP regular expression summary
The examples in this article describe a summary of 10 practical PHP regular expressions and share them with you for your reference. The details are as follows:
Regular expressions are an important element in program development. They provide strings used to describe or match text, such as specific characters, words, or calculations. But in some cases, using regular expressions to validate a string is complex and time-consuming. This article introduces you to 10 common and practical ways of writing PHP regular expressions. I hope it will be helpful to your work.
1. Verify email address
This is a regular expression used to validate emails. But it's not an efficient, perfect solution. Not recommended here.
Copy code The code is as follows:
$email = "test@ansoncheung.tk";
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";
}
In order to verify email addresses more effectively, it is recommended to use filer_var.
Copy code The code is as follows:
if (filter_var('test+email@ansoncheung', FILTER_VALIDATE_EMAIL)) {
echo "Your email is ok.";
} else {
echo "Wrong email address format.";
}
2. Verify username
This is an example for validating a username, which includes letters, numbers (A-Z, a-z, 0-9), underscores, and a minimum of 5 characters and a maximum of 20 characters. At the same time, you can also make reasonable modifications to the minimum and maximum values as needed.
Copy code The code is as follows:
$username = "user_name12";
if (preg_match('/^[a-zd_]{5,20}$/i', $username)) {
echo "Your username is ok.";
} else {
echo "Wrong username format.";
}
3. Verify phone number
This is an example of verifying a US phone number.
Copy code The code is as follows:
$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 example used to verify IPv4 addresses.
Copy code The code is as follows:
$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 zip code
This is an example used to verify the zip code.
Copy code The code is as follows:
$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 the US SSN.
Copy code The code is as follows:
$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
Copy code The code is as follows:
$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 domain name
Copy code The code is as follows:
$url = "http://ansoncheung.tk/";
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. Extract domain name from specific URL
Copy code The code is as follows:
$url = "http://ansoncheung.tk/articles";
preg_match('@^(?:http://)?([^/]+)@i', $url, $matches);
$host = $matches[1];
echo $host;
10. Highlight the keywords in the text
Copy code The code is as follows:
$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;
I hope this article will be helpful to everyone’s PHP programming design.
[^\d](?\d{10})([^\d]|$)
int preg_match ( string $pattern , string $subject [, array $matches [, int $flags ]] )
string $pattern is a string, so you can first calculate the string and store it in a variable
$star='a';
$stop='c';
$info='a1b2c3';
$pattern='/'.$star.'(.+?)'. $stop.'/';
preg_match($pattern,$info,$result);
print_r($result[1]);
http://www.bkjia.com/PHPjc/898284.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/898284.htmlTechArticle10 practical PHP regular expressions summary, php regular expression summary This article tells 10 practical PHP regular expressions with examples A summary of regular expressions, shared with everyone for your reference. The details are as follows: Regular...