PHP Regular Expressions
php-regular-expressions
php-regular-expressions
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"); ?>
[[: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
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 } ?>
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); ?>
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; } } ?>
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.
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"; } ?>
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().
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.
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:
PHP provides POSIX extended and Perl Compatible built-in functions to work with regular expressions.
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: