Home / PHP/MySQL Tutorials / Archive by category 'PHP Regular Expressions'

PHP Regular Expressions

php-regular-expressions

Validate email address in PHP

To do simple validation on an e-mail address, we can use the following regular expression. (The double escapes are for double quotation marks; use single escapes for single quotation marks.)

<?php
$email = "a@aa.com";
if (!eregi ("^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}$", $email))
    die ("Invalid E-mail");
?>

PHP character classes for ereg

[[:alnum:]]
All alphanumeric characters [a-zA-Z0-9]

[[:alpha:]]
All alphabetic characters [a-z]

[[:blank:]]
Tab and space [\t ]

[[:cntrl:]]
All the control characters

[[:digit:]]
All decimal digits [0-9]

[[:graph:]]
All printable characters except space

[[:lower:]]
All lowercase letters [a-z]

[[:print:]]
All printable characters

[[:punct:]]
Punctuation marks [\.,;:-]

[[:space:]]
All whitespace characters

[[:upper:]]
All the uppercase letters [A-Z]

[[:xdigit:]]
The set of hexadecimal digits


Pattern matching for alphabetic characters in PHP

There are two ways to check whether a value contains only alphabetic characters.

Example to use eregi() in conjunction with character classes:

<?php
if (eregi ("^[a-z]+$", $line)) {
    // .. true
} else {
    // .. false
}
?>

Example to use the predefined character class along with ereg():

<?php
if (ereg ("^[[:alpha:]]+$", $line)) {
    // .. do if true
} else {
    // .. do if false
}
?>

PCRE regex match multiple lines in PHP

Example:

<?php
$text = "Review all the potential problems and eliminate any 
that are improbable or whose consequences would be insignificant. 
Show the problems as a fourth level linked to the tasks.";
 
preg_match ("/problems$/m", $text, $matches);
?>

Regular expression for matching over multiple lines in PHP

It might not be easy to match over multiple lines without PCRE installed, but it is possible.

Example:

<?php
$text = "Review all the potential problems and eliminate any 
that are improbable or whose consequences would be insignificant. 
Show the problems as a fourth level linked to the tasks.";
 
$lines = explode ("\n", $text);
 
foreach ($lines as $line) {
    if (ereg ('problems$', $line)) {
        $matches[] = $line;
    }
}
?>

Find pathnames matching a pattern using PHP Glob()

The glob() function searches for all the pathnames matching pattern according to the rules used by the libc glob() function, which is similar to the rules used by common shells.

<?php
foreach (glob('/www/public_html/files/*.txt') as $file) {
   $contents = file_get_contents($file);
   print "$file contains $contents\n";
}
?>

The glob() function returns an array of matching full pathnames. If no files match the pattern, glob() returns false.


Find all filenames that match a pattern using a FilterIterator

The following code illustrates the use of a FilterIterator subclass with DirectoryIterator.
The FilterIterator subclass needs its own accept() method that decides whether or not a particular value is acceptable.

<?php
class ImgFilter extends FilterIterator {
    public function accept() {
        return preg_match('@\.(gif|jpe?g|png)$@i',$this->current());
    }
}
 
foreach (new ImgFilter(new DirectoryIterator('/www/public_html/images')) as $img) {
    print "<img src='".htmlentities($img)."'/>\n";
}
?>

Regular expressions in PHP : Part 4 – Perl Compatible functions

Perl Compatible Regular Expressions have an acronym of PCRE. These PHP functions closely follow the syntax used in Perl regular expressions. These functions tend to be slightly faster than their POSIX compatible relatives.

preg_replace() searches for $pattern in $subject and relaces it with $replacement. Here, mixed indicates that a parameter may accept multiple (but not necessarily all) types.

mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject )

preg_match() searches $subject for a match to the regular expression given in $pattern.

int preg_match ( string $pattern , string $subject )

preg_grep() returns the array consisting of the elements of the input array that match the given pattern.

array preg_grep ( string $pattern , array $input )

PHP also supports some other functions like preg_match(), preg_split() and preg_quote().


Regular expressions in PHP : Part 3 – POSIX extended functions

POSIX or “Portable Operating System Interface for Unix is the name of a family of related standards specified by the IEEE to define the application programming interface (API), along with shell and utilities interfaces for software compatible with variants of the Unix operating system, although the standard can apply to any operating system.The following POSIX extended functions are provided by PHP:

ereg_replace(): This function is used to search for $pattern in $string and replace it with $replacement.

string ereg_replace ( string $pattern , string $replacement , string $string )

ereg(): This function searches for $pattern in $string in a case sensitive manner.

int ereg ( string $pattern , string $string [, array &$regs ] )

The eregi_replace() and eregi() functions are similar to the above, but are case insensitive.


Regular expressions in PHP : Part 2 – Basic syntax of regular expressions

PHP provides standard syntax to group the characters inside a pattern. PHP employs special characters with different meanings when used in regular expressions. The folowing syntax patterns are provided by PHP:

  • Normal characters which form a string like “car”.
  • Start indicator ^ and the end indicator $. “^car” matches the pattern if it is present at the beginning of the subject and “car$” matches the pattern if it is present at the end of the subject.
  • Count indicators like +,*,?. For example, world* searches for a pattern with 0 or more d after worl. world+ searches for a pattern with atleast 1 d after worl. world? searches for a pattern with 0 or 1 d after worl.
  • The logical operator |. “car|bus” searches for a pattern which contains car or bus.
  • Grouping of characters can be done with {},() and []. For example, world{2,3} searches for a pattern with 2 or 3 d after worl. [abc] searches for a pattern with abc in the subject. [a-z] searches for a pattern with any lower case letters in the string. wo(rld) searches for a pattern with rld after wo.
  • PHP provides POSIX extended and Perl Compatible built-in functions to work with regular expressions.


Regular expressions in PHP : Part 1 – Introduction

Regular expressions provide a flexible means of identifying the required text such as characters, words or patterns of characters. Using regular expressions the required text can be searched and manipulated if needed. Regular expressions are slower than the string functions. However, they provide are a powerful tool for text manipulation.

PHP supports two types of regular expressions:

  • POSIX Extended
  • Perl Compatible