Some useful PHP, Perl, and JavaScript Regular Expressions

A regular expression, also called regex or regexp for short, is simply a piece of code that matches a pattern. Mastering regular expressions can be a difficult chore, and if you don’t need them all of the time, the syntax is tricky enough to make the task frustrating or slow as you will constantly need to use a reference sheet.

In order to save you time, I’ve compiled a list of PHP, Perl, and JavaScript regular expressions for common use cases that have been tested and are ready to go. This isn’t a regular expression tutorial or even a reference; you can think of it more as a cheatsheet for when you just need the regex but don’t want to put a lot of time into relearning regular expressions.

 

Perl and PHP Regular Expressions

PHP regexes are based on the PCRE (Perl-Compatible Regular Expressions), so any regexp that works for one should be compatible with the other or any other language that makes use of the PCRE format. Here are some commonly needed regular expressions for both PHP and Perl. Each regex will be in string format and will include delimiters.

All Major Credit Cards

This regular expression will validate all major credit cards: American Express (Amex), Discover, Mastercard, and Visa.

  1. //All major credit cards regex
  2. ‘/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|622((12[6-9]|1[3-9][0-9])|([2-8][0-9][0-9])|(9(([0-1][0-9])|(2[0-5]))))[0-9]{10}|64[4-9][0-9]{13}|65[0-9]{14}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})*$/’

Alpha-Numeric Characters

Test for alpha-numeric characters with this regexp.

  1. //Alpha-numeric characters only
  2. ‘/^[a-zA-Z0-9]*$/’

Alpha-Numeric Characters With Spaces

Test for alpha-numeric characters and spaces with this regexp.

  1. //Alpha-numeric characters with spaces only
  2. ‘/^[a-zA-Z0-9 ]*$/’

Alphabetic Characters

This regex will test for alphabetic characters only (upper and lowercase).

  1. //Alphabetic characters only
  2. ‘/^[a-zA-Z]*$/’

 

Date (MM/DD/YYYY)

Validate the calendar date in MM/DD/YYYY format with this regex. Optional separators are spaces, hyphens, forward slashes, and periods. The year is limited between 1900 and 2099.

  1. //Date (MM/DD/YYYY)
  2. ‘/^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2})*$/’

Date (YYYY/MM/DD)

Validate the calendar date in YYYY/MM/DD format with this regex. Optional separators are spaces, hyphens, forward slashes, and periods. The year is limited between 1900 and 2099.

  1. //Date (YYYY/MM/DD)
  2. ‘#^((19|20)?[0-9]{2}[- /.](0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01]))*$#’

Digits

This regex will test for digits (whole numbers).

  1. //Digits only
  2. ‘/^[0-9]*$/’

 

Emails

This email regex is not fully RFC5322-compliant, but it will validate most common email address formats correctly.

  1. //Email regex
  2. ‘/^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})*$/’

IP Addresses

Test IP Addresses with this regular expression.

  1. //IP address regex
  2. ‘/^((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))*$/’

Lowercase Alphabetic Characters

This regex will test for lowercase letters.

  1. //Lowercase letters only
  2. ‘/^([a-z])*$/’

 

Uppercase Alphabetic Characters

This regex will test for uppercase letters.

  1. //Uppercase letters only
  2. ‘/^([A-Z])*$/’

URLs

This URL regex will validate most common URL formats correctly.

  1. //URL regex
  2. ‘/^(((http|https|ftp):\/\/)?([[a-zA-Z0-9]\-\.])+(\.)([[a-zA-Z0-9]]){2,4}([[a-zA-Z0-9]\/+=%&_\.~?\-]*))*$/’

 

PHP snippet : Number of digits of an integer

This is a basic PHP program that returns the number of digits of an integer.

[php]
<?php
//This is a simple function to return number of digits of an integer.

//function declaration
function count_digit($number)
{
$digit = 0;
do
{
$number /= 10; //$number = $number / 10;
$number = intval($number);
$digit++;
}while($number!=0);
return $digit;
}

//function call
$num = 12312;
$number_of_digits = count_digit($num); //this is call 🙂
echo $number_of_digits;
//prints 5
?>
[/php]

PHP tutorial : Basic PHP tags

We can use several types of tags to define the start and end of PHP code. I have illustrated “few” of them that worked for me. The first one is basic echo with No tags, so it will just output the echo.
[php]echo "No Tags"
[/php]

The second one is the most commonly used tag in PHP. It is the widely accepted standard.
[php]
<?php echo "This is the most commonly used tag" ?>
[/php]

The third , is used very frequently , (Haven’t used this in WordPress though)
[php]
<? echo " Another tag "?>
[/php]
And the last, is the old style (familiar with javacript declarations)

[php]
<script language="php">
echo "this is php using script language"
</script>
[/php]