Anda di halaman 1dari 3

Regex #1.

Email Validation
The email validation regular expression (regex) is the most
frequently used PHP regex. Here is a regex that can match
99 percent of email addresses.
/[a-z0-9]+([_.-][a-z0-9]+)*@([a-z0-9]+([.-][a-
z0-9]+)*)+.[a-z]{2,}/i
1.1 Regex Returns
This regex will return a valid formatted email address. The
local part (before the @ sign) must start with a letter and
can contain only letters, numbers, dots and hyphens. The
domain name (after the @ sign) can contain alphanumeric
characters and hyphens and must have a dot and at least
two letters after it.
1.2 PHP Example
<?php
$e-mail=username@domain.com;
if(preg_match(/[a-z0-9]+([_\.-][a-z0-9]+)*@
([a-z0-9]+([.-][a-z0-9]+)*)+\.[a-z]{2,}/i,$e-
mail)) {
echo E-mail is valid.;
}
else {
echo E-mail not valid.;
}
?>
1.3 Test Results
Regex #2. Username Validation
This one is a rather simple but widely used regular expression.
Many websites allow user registration, so they need to parse
the usernames safely to prevent security problems.
/^[a-zA-Z0-9_-]{3,20}$/i
2.1 Regex Returns
This expression will return a username, which can contain
only alphanumeric characters, underscores and hyphens. Its
minimum length is 3 characters, and the maximum length is 20.
2.2 PHP Example
<?php
$username=username;
if(preg_match(/^[a-zA-Z0-9_-]
{3,20}$/i,$username)) {
echo Username is valid.;
}
else {
echo Username not valid.;
}
?>
2.3 Test Results
username@domain.com true
username@domain.com.br true
user_name@domain.com true
!user_name@domain.com false
username@domain.com false
I suggest checking the MX record after checking the
address format, because the regular expression cannot
diferentiate invalid from valid domains.
username true
ab false
abc true
User_name true
Username! false
Regex #3. Highlight a Word in Text
This is a widely used regex in search results, e.g.
highlighting keywords. Note that it will highlight whole
words only.
/b(word-to-be-replaced)b/i
3.1 Regex Returns
The expression returns a word between the brackets.
3.2 PHP Example
<?php
$text = This is a sample text, in which we will
highlight a word sample.
Because this regex highlights whole words only,
the word samples will not be highlighted.;
preg_match(/b(sample)b/i, <span
style=background:red >sample</span>, $text);
echo $text;
?>
Regex #4. Select an HTML Tag
Used together with cURL, this is a powerful regex that
has many possibilities. However, do not use it for fetching
copyrighted content.
{<.$tag.[^>]*>(.*?)</.$tag.>}
4.1 Regex Returns
The return of this regex is the innerHTML value of the tag.
Tags themselves are not returned.
4.2 PHP Example
<?php
$html = fle_get_contents(http://www.domain.
com);
$tag=title; //we will extract page title
preg_match({<.$tag.[^>]*>(.*?)</.$tag.>},
$html,$match);
$page_title = $match[0];
echo $page_title;
?>
Top 10 PHP Regex Cheat Sheet
Copyright Quinstreet 2011
Regex #5. Select an HTML Tag with
an Attribute Value
This regex is very similar to the previous one, but it allows
you to defne an attribute and its value and select only an
HTML tag with that value.
/<(.$tag.)[^>]*$attrs*=s*([])$val
ue\2[^>]*>(.*?)</\1>/
5.1 Regex Returns
The return of this regex is the innerHTML value of the tag.
Tags themselves are not returned.
5.2 PHP Example
<?php
$html = fle_get_contents(http://www.domain.
com);
$tag = div;
$attr = id;
$value = container;
preg_match(/<(.$tag.)[^>]*$attrs*=s*([])$va
lue\2[^>]*>(.*?)</\1>/,$html,$match);
$page_content = $match[0];
?>
Regex #6. Password Complexity
The notifcation for a user to create a strong password
is now standard. A strong password must be at least six
characters long and contain at least one uppercase letter,
one lowercase letter and one digit.
/A(?=[-_a-zA-Z0-9]*?[A-Z])(?=[-_a-zA-Z0-
9]*?[a-z])(?=[-_a-zA-Z0-9]*?[0-9])S{6,}z/
6.1 Regex Returns
This regex will return true if the password is strong and
false if the password is weak.
6.2 PHP Example
<?php
$pass=Pass123;
if(preg_match(/A(?=[-_a-zA-Z0-9]*?[A-Z])
(?=[-_a-zA-Z0-9]*?[a-z])(?=[-_a-zA-Z0-9]*?[0-9])
S{6,}z/,$pass)) {
echo Pass strong.;
}
else {
echo Pass weak.;
}
?>
6.3 Test Results
7.2 PHP Example
<?php
$cc_num=5130789134893478;
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_num)) {
echo Credit card valid.;
}
else {
echo Credit card not valid.;
}
?>
7.3 Test Results
pass weak
Password weak
Pass123 strong
PassWord weak
PassWord1 strong
xxxx xxxx xxxx xxxx false White spaces are not
allowed
4xxxxxxxxxxxxx true Visa card number format
51xxxxxxxxxxxxxx true Mastercard
36xxxxxxxxxxxx true Diners Club
6011xxxxxxxxxxxx true Discover
Regex #7. Credit Card Number
Validation
Whether you are running an online store or building a
custom shopping cart, you will fnd this regex useful. It
matches all major credit cards.
/^(?: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})$/
7.1 Regex Returns
The regex will return a credit card number if valid, or false if
not valid.
Regex #8. Phone Number Validation
When running a classifeds website or receiving customer
enquiries, it is important that users enter their phone
numbers correctly. Note that this regex matches U.S.
phone numbers only.
/(?d{3})?[-s.]?d{3}[-s.]d{4}/x
8.1 Regex Returns
If the phone number is valid, the regex will return the
whole number. Otherwise, it returns false.
Copyright Quinstreet 2011
Top 10 PHP Regex Cheat Sheet Page 2
8.2 PHP Example
<?php
$phone_num=(012)123-4567;
if(preg_match(/(?d{3})?[-s.]?d{3}[-s.]d{4}/
x,$phone_num)) {
echo Phone number valid.;
}
else {
echo Phone number not valid.;
}
?>
8.3 Test Results
10.2 PHP Example
<?php
(012)123-4567 true
(012) 123-4567 true
(012) 1234567 false
(012) 123.4567 true
(012) 123 4567 true
Regex #9. Validate a Domain Name
This regex will check the domain name format.
/^(http|https|ftp)://([A-Z0-9][A-Z0-9_-]*(?:.[A-
Z0-9][A-Z0-9_-]*)+):?(d+)?/?/i
9.1 Regex Returns
The full domain is returned if valid; otherwise false is returned.
Top 10 PHP Regex Cheat Sheet Page 3
9.2 PHP Example
<?php
$domain=http://www.domain.com;
if(preg_match(/^(http|https|ftp)://([A-
Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-
]*)+):?(d+)?/?/i,$domain)) {
echo Domain valid.;
}
else {
echo Domain not valid.;
}
?>
9.3 Test Results
Regex #10. Find a URL in a String
This regex allows you to block a URL, for example, in
forum posts or article comments.
/(http|https|ftp)://([A-Z0-9][A-Z0-9_-]*(?:.[A-
Z0-9][A-Z0-9_-]*)+):?(d+)?/?/i
10.1 Regex Returns
If the regex is used with the preg_match() function, it will
return the frst URL found in a string. In order to return all
the URLs, use the preg_match_all() function. If no domain
is found, false will be returned.
http://www.domain.com true
http://domain.com true
www.domain.com false
domain.com false
https://www.domain.com true
ftp://domain.com true
Copyright Quinstreet 2011
http://www.domain.com true
http://domain.com true
www.domain.com false
domain.com false
https://www.domain.com true
ftp://domain.com true

Anda mungkin juga menyukai