PHP regex tutorial, examples using the Head First PHP book

PHP regex tutorial, examples using the Head First PHP book

Check date of birth, something that should result into: 1985.

1
2
if (!preg_match('/^\d{4}$/', $birth))
//asks for 4 digits

or another variation:

2
if (!preg_match('/^[0-9]{4}$/', $birth))

or even more exact:

3
4
if (!preg_match('/^[1-2]{1}[0-9]{1}[0-9]{1}[0-9]{1}$/', $birth))
//will ask that the first digit is either 1 or 2, the rest you understand easily.

**************************************************************

Checks the name:

4
5
if (!preg_match('/[a-zA-Z]/', $name))
//checks if the $name contains only letters. Can't contain digits.

This one:

5
6
!preg_match('/^[a-zA-Z]$/', $name))
//will not allow the first or last character to not be a letter (^ and $ - first char/last char).

**************************************************************

Another example with preg_split:

6
7
8
9
$string = 'Hello there < friend';
$string = preg_split('/</', $string);
echo ''.$string[1];
//will echo friend.

but let’s say you have this string:

7
$string = 'Hello there1 < friend';

As you see, there is a “1” merged with “there”.
We can use a regex here.

8
9
10
11
$string = 'Hello there1 < friend';
$string = preg_split('/[a-z]{5}\d{1}/', $string);
echo ''.$string[1].'';
//it will echo < friend.

to echo just “friend”, use:

9
10
11
12
$string = 'Hello there1 < friend';
$string = preg_split('/[a-z]{5}\d{1} < /', $string);
echo ''.$string[1].'';
//it will echo < friend.

*********************************************************

Let’s say you want to preg_match for a website address, something like www.incvice.com, it can be www.google.com or whatever. The pattern is that it should start with www. and end with .com or .net or .org or whatever.

Here is the preg_match code:

10
11
if (preg_match('/www.[a-z].com/');
//only works for .com

*********************************************************

This script will echo all the web addresses from the string, using the pattern.

11
12
13
14
15
16
17
18
$string = 'These are some great websites: www.google.com, www.incvice.com, www.yahoo.com.';
 
preg_match_all('/www.[a-z]+.[a-z]+/', $string, $matches);
 
foreach ($matches[0] as $match)
	{
		echo ''.$match.'<br>';	
	}

*********************************************************

Checks if there is an e-mail address in a string:

12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
 
$string = 'admin@incvice.com';
 
preg_match_all('/[a-z0-9]+@[a-z]+.[a-z]+/' ,$string, $matches);
 
foreach ($matches[0] as $match)
	{
		$new_array[] = $match;
	}
 
echo ''.$new_array[0].'';
 
?>

another example:

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
 
$string = 'admin%incvice.com';
 
if (preg_match('/[a-z0-9]+@[a-z]+.[a-z]+/', $string))
		{
 
preg_match_all('/[a-z0-9]+@[a-z]+.[a-z]+/' ,$string, $matches);
 
foreach ($matches[0] as $match)
	{
		$new_array[] = $match;
	}
 
echo ''.$new_array[0].'';
 
		}
else
	{
		echo 'there is no e-mail address in the string';	
	}
 
?>

However, the regex syntax for e-mails, is:

14
if (!preg_match('/^[a-zA-Z0-9][a-zA-Z0-9\._\-&!?=#]*@/', $email)) {

Add a comment: