Posted by: phillipnb | April 2, 2011

php and regular expressions – part 3


Continuing our discussion about regular expressions from the last edition, let us work on a few more examples:

Example 1

Suppose a user wanted to match every full name, first name and last name. To achieve this, we write the pattern as shown below.

$subject = "David Snowman";
$pattern = "/(\w+)\s(\w+)/";
$matches = array();
findMyPattern($pattern, $subject, $matches);

The output of this will be:

Array
(
    [0] => Array
        (
            [0] => David Snowman
        )
    [1] => Array
        (
            [0] => David
        )
    [2] => Array
        (
            [0] => Snowman
        )
)

Pay attention to the way sub-pattern (using ()) matching is done.

Example 2

In this example we try to match any word that starts with a ‘D’

$subject = "David Snowman Doberman Droppy Walter Dumpty Dorothy";
$pattern = "/D[a-z]+\s/";
$matches = array();
findMyPattern($pattern, $subject, $matches);

The output of this code will be:

Array
(
    [0] => Array
        (
            [0] => David 
            [1] => Doberman 
            [2] => Droppy 
            [3] => Dumpty 
        )
)

If you take a look at $subject and the output, you can see that ‘Dorothy’ was omitted. The reason for this is the ‘\s’ at the end of the pattern which forces the pattern to look for those names that start with ‘D’ but has a space at the end. For this example, if we change the pattern to $pattern = “/^D[a-z]+\s/”; then the output will be ‘David’ because by using ‘^’ we are asking php to match only at the beginning of $subject. The reverse is also true. If we use the character ‘$’, we are asking php to match at the end of the string. Suppose our pattern is $pattern = “/D[a-z]+$/”; then the output will be ‘Dorothy’ because the match is at the end of $subject and ‘Dorothy’ is the only name that satisfies the $pattern.

Example 3

$subject = 'Ron Lowe';
$pattern = "/[0-9,;%#@]+/";

if (preg_match_all($pattern,$subject,$matches)) {
    print_r($matches);
}
else {
    echo "\n No match";
}

Consider the above pattern where we want to match a valid name. In the $pattern I have put everything that I do not want to see in a name like numbers, comma, semicolon, etc. If we run this code the output will be the text ‘No match’ which tells me that it is a valid name

Example 4

Suppose we want to check if the string is a valid telephone number. Let us assume that the format for a valid telephone number is 000-000-0000. Create a pattern to test this,

$subject = '012-345-6789';
$pattern = "/[0-9]{3}-[0-9]{3}-[0-9]{4}/";

if (preg_match_all($pattern,$subject,$matches)) {
    print_r($matches);
}
else {
    echo "\n Invalid Telephone";
}

What is written using the above pattern is that – match a string which has numbers repeated 3 times (or 3 numbers) followed by a hyphen, then again match 3 numbers followed by a hyphen followed by 4 numbers. The output for this code will be 012-345-6789

Example 5

Suppose we want to match if the given email is valid (which is made of alphabets,numbers etc followed by ‘@’,followed by domain name which is again made of alphabets numbers etc, a dot and finally ending in ‘org’ or ‘com’)

$subject = 'jill@mangoorg';
$pattern = "/[A-Za-z0-9]+@[A-za-z0-9]+\.org|com$/";

if (preg_match_all($pattern,$subject,$matches1)) {
    print_r($matches);
}
else {
    echo "\n Invalid";
}

The output for the above example will be ‘Invalid’ as the email is missing a dot before ‘org’.

Till now we have played with a number of regular expression patterns. Hope, this will help you to write your own patterns using regular expressions. So, till next time, it is Happy PHPing

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: