Posts Tagged Regular Expressions

Regular Expressions for PHP Developers

Regular expressions syntax

Regular Expression Action
xyz The string “xyz”
^xyz “xyz” at the start of a string
xyz$ “xyz” at the end of a string
^xyz$ “xyz” when it is alone on a string
[abc] a, b, or c
[a-z] Any lowercase letter
[^A-Z] Any character not included a uppercase letter
(gif|jpeg) Matches either “gif” or “jpeg”
[a-z]+ One or more lowercase letters
[0-9.-] Any number, dot, or minus sign
^[a-zA-Z0-9_]{2,}$ Any word of at least two letter, number or _
([wx])([yz]) wy, wz, xy, or xz
[^A-Za-z0-9] Any Symbols (No Numbers or Letters, Only Symbols)
([A-Z]{4}|[0-9]{5}) Matches four letters or five numbers

PHP regular expression functions

Function Description
preg_match() The preg_match() function searches string for pattern, returning true if pattern exists, and false otherwise.
preg_match_all() The preg_match_all() function matches all occurrences of pattern in string.
preg_replace() The preg_replace() function operates just like ereg_replace(), except that regular expressions can be used in the pattern and replacement input parameters.
preg_split() The preg_split() function operates exactly like split(), except that regular expressions are accepted as input parameters for pattern.
preg_grep() The preg_grep() function searches all elements of input_array, returning all elements matching the regexp pattern.
preg_quote() Quote regular expression characters

1) String Match

<?php
function alphaNumericCheck($element) {
return !preg_match ("/[^A-z0-9_\-]/", $element);
}
?>


2) Password Match Validation

<?php
$password = "Fyfjk34sdfjfsjq7";
if (preg_match("/^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/", $password)) {
echo "Your passwords is strong.";
} else {
echo "Your password is weak.";
}
?>


3) Email Address Validation
<?php

$email = “someone@example.com”;

if(eregi(“^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$”, $email)) {

echo “Valid email address.”;

}

else {

echo “Invalid email address.”;

}

?>


4) Date Format Validation

<?php
function checkDateFormat($date)
{
//match the format of the date
if (preg_match ("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date, $parts))
{
//check weather the date is valid of not
if(checkdate($parts[2],$parts[3],$parts[1]))
return true;
else
return false;
}
else
return false;
}
//Usage :
print_r(checkDateFormat("2008-02-29")); //return true
print_r(checkDateFormat("2007-02-29")); //return false
?>


5) Validate URL

^(?:https?:\/\/)?(?:[a-z0-9-]+\.)*((?:[a-z0-9-]+\.)[a-z]+)
Explanation
^ # start-of-line
(?: # begin non-capturing group
https? # "http" or "https"
:// # "://"
)? # end non-capturing group, make optional
(?: # start non-capturing group
[a-z0-9-]+\. # a name part (numbers, ASCII letters, dashes) & a dot
)* # end non-capturing group, match as often as possible
( # begin group 1 (this will be the domain name)
(?: # start non-capturing group
[a-z0-9-]+\. # a name part, same as above
) # end non-capturing group
[a-z]+ # the TLD
) # end group 1

Validating URL using regular expressions:

function isValidURL($url) {
return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}

Validating URL using filter_var:

function isValidURL($url) {

if (filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) return true;
else return false;
}

Validating Email using regular expressions:
function isValidEmail($email) {
return preg_match(“^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^”, $email);
}

Validating Email using filter_var:
function isValidEmail($email) {
if (filter_var($email, FILTER_VALIDATE_EMAIL)) return true;
else return false;
}


6) Ip address validation in PHP using regular expression


function validateIpAddress($ip_addr)
{
//first of all the format of the ip address is matched
if(preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/",$ip_addr))
{
//now all the intger values are separated
$parts=explode(".",$ip_addr);
//now we need to check each part can range from 0-255
foreach($parts as $ip_parts)
{
if(intval($ip_parts)>255 || intval($ip_parts)<0)
return false; //if number is not within range of 0-255
}
return true;
}
else
return false; //if format of ip address doesn't matches
}


7) Get all images from a HTML document

 

 


8) Remove repeated words (case insensitive)

 


9) Remove repeated punctuation


10) Matching a XML/HTML tag

 

 


11) Matching an XHTML/XML tag with a certain attribute value

 

Posted in: Uncategorized, Web Developers/Designers Reference Guide

Leave a Comment (0) →