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"); ?>
Continue reading…

Enjoyed this post? Share it!

 
 

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] […]
Continue reading…

Enjoyed this post? Share it!

 
 

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)) { […]
Continue reading…

Enjoyed this post? Share it!

 
 

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); […]
Continue reading…

Enjoyed this post? Share it!

 
 

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. […]
Continue reading…

Enjoyed this post? Share it!

 
 

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"; […]
Continue reading…

Enjoyed this post? Share it!

 
 

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 […]
Continue reading…

Enjoyed this post? Share it!

 
 

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 […]
Continue reading…

Enjoyed this post? Share it!

 
 

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 […]
Continue reading…

Enjoyed this post? Share it!

 
 

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 […]
Continue reading…

Enjoyed this post? Share it!