Introduction To Regular Expressions In PHP

January 25th, 2017

In Linux and Unix, the syntax that is commonly used by many applications for specifying text patterns is known as regular expressions or in short form – regex. Regex is a very powerful technique to describe patterns and many programs use them to describe sequences of characters to be matched. Search programs such as ‘grep’ rely heavily on regex. Basically regex forms the core in the linux world. Many scripting languages such as perl, ruby, php…etc has build in regex functions as well. So you can see, learning regular expression is important because they are used a lot in many places and probably more so in the future.

Regex can be scary at first but if you can get the basics, it is really not too hard to understand. In this article, we are going to look at how regex comes into the picture when writing php applications.

To do a quick summary so far, a regular expression is a sequence of literal characters, wildcards, modifiers and anchors.

Literal Characters

Literal characters are letters, digits and special characters that match only themselves. Examples are abc, 123, ~@ and so on (some characters are reserved though).

– An inclusion range [m-n] matches one of any character included in the range from m to n.

Example ‘[a-z]’ will match any alpha character that falls within the a to z range.

– An exclusion range [^m-n] matches one of any character not included in the range from m to n. Example ‘[^0-9]’ will match any non-digit character.

– A period “.” matches any character. It is also known as the wildcard. Example ‘a.c’ will match ‘aec’, ‘acc’, ‘a@a’ and so on.

– The escape character ” enable interpretation of special characters. Example ‘a.c’ will match ‘ac’ only. Remember that ‘.’ is a reserved character to represent a wildcard? Therefore to match a period, ie ‘.’, we need to escape it like so ‘.’

– The expression [:alnum:] will match all alpha-numeric characters. It is a shortcut to [A-Za-z0-9]. As you can see, it is not really a shortcut. The expression [:alnum:] might be easier to remember for some people.

– The expression [:alpha:] will match all alpha characters. It is a shortcut to [A-Za-z].

– The expression [:blank:] will match a space or tab.

– The expression [:digit:] will match a numeric digit. It is a shortcut to [0-9].

– The expression [:lower:] will match all lowercase letters. It is a shortcut to [a-z].

– The expression [:upper:] will match all uppercase letters. It is a shortcut to [A-Z].

– The expression [:punct:] will match all printable characters, excluding spaces and alphanumerics.

– The expression [:space:] will match a whitespace character.


A modifier alters the meaning of the immediately preceding pattern character.

– An asterisk (‘*’) matches 0 or more of the preceding term. Example ‘a*’ will match ”, ‘a’, ‘aa’, ‘aaaaa’ and so on (Note the use of ”. It simply means that the expression matches nothing as well).

– A question mark (‘?’) matches 0 or 1 of the preceding term. Example ‘a?’ will match ” and ‘a’ only.

– A plus sign (‘+’) matches 1 or more of the preceding term. Example ‘a+’ will match ‘a’, ‘aaaaaaa’ and so on. It will not match ”.

– {m,n} matches between m and n occurrences of the preceding term. Example ‘a{1,3}’ will match ‘a’, ‘aa’ and ‘aaa’ only.

– {n} matches exactly n occurrences of the preceding term. Example ‘a{2}’ will match ‘aa’ only.


Anchors establish the context for the pattern such as “the beginning of a word” or “end of word”.

– The pike ‘^’ marks the beginning of a line. Example ‘^http’ will match any new line that starts with ‘http’.

– The dollar sign ‘$’ marks the end of a line. Example ‘after$’ will match any line that ends with ‘after’. (Variables in php starts with $. Try not to confuse with it).


Grouping ‘( )’ allows modifiers to apply to groups of regex specifiers instead of only the immediately proceding specifier. Example ‘( aa | bb )’ will match either ‘aa’ or ‘bb’

Enough of boring stuff, it is time to put what the theory of regex into good use.

PHP Implementation

There are 2 main variants of regex, Perl-compatible regex (PCRE) and POSIX-Extended. PHP offers quite a lot of functions to implement these 2 types of regex. In PHP, the most commonly used PCRE function is ‘preg_match’ and in POSIX-extended regex, ‘ereg’. Both syntax are slightly different but equally powerful. The preference to use ‘preg_match’ or ‘ereg’ is entirely up to individual although Zend suggested that preg_match is slightly faster. I prefer to use ‘eregi’ simply because of my background in linux administration.

Example 1: Matching United States 5 or 9 digit zip codes

Zip codes in USA have the following format ##### or #####-#### where # is a digit. If you want to verify the zip code submitted say from an online form, you will need to use regex somewhere in your script to verify it. The matching POSIX-extended regex pattern will be:


Confused? Wait, let me explain why. This regex is split up into 2 parts: [[:digit:]]{5} and (-[[:digit:]]{4})?.

First Part: ‘[[:digit:]]’ means the digit range and {5} means that the digit must occur 5 times.

Second Part: The bracket ‘( )’ groups the ‘-[[:digit:]]{4}’ together and the ‘?’ means the expression ‘(-[[:digit:]]{4})’ can either occur 0 or 1 time.

To implement the regex in PHP, we use the following code:

$zipCodes = ‘xxxxx-xxxx’;

$pattern = ‘[[:digit:]]{5}(-[[:digit:]]{4})?’;

if (ereg($pattern,$zipCodes)) {

echo “matched found “;


else {

echo “match not found”;


Example 2: Matching Dates

Say we want to verify the dates entered by the user. If we only accept dates like “YYYY-MM-DD” or “YYYY-M-D”, the regex pattern will be


The ‘+’ behind the term (-[0-9]{1,2}) means that the term must occur at least once. Note that I can also rewrite the regex as:




As you can see, there can be many solutions to a problem…


Regex may be hard to digest at first but the logic is simple if you are able to practice more. Learning regex is as important as learning PHP. More examples can be seen at Good luck.

More Helpful Information on Pink Digital Cameras

January 17th, 2017

Ever since cameras were invented, they have gone through a revolution of sorts. They have evolved from large weird-looking gadgets to small thin and sleek items that can fit into any purse or breast-pocket. While previous years’ cameras were dull-looking, today’s cameras have taken on a sheek innovative and even futuristic look. With users demanding even more uniqueness and expression, digital companies such as Canon, Fuji, Panasonic Lumix, Casio and JVC have put on their creative shoes and gone all out. Pink digital cameras as the result and we can expect more.

There is usually a lot of hype associated with the release of a new digital product and people usually get disappointed afterwards when it does not match the reviews. This then causes bad reviews which then result in negative publicity. But this has not happened with pink digital cameras. These have been an instant sensation from day one. This is because other than just their visual appeal, the devices came with a load of additional user-friendly features and options. Their high-tech functionality allowed users to take quality digital shots in any type of lighting.

Talking of functionality, pink digital cameras come with great additional features. These allow the user to take great quality photographs in any setting. Professional shots are also possible with the cameras. The Olympus Stylus comes with 10 megapixel capability. This allows for professional-looking pictures for web and print. The Casio Exilim EX-Z75 7.2 megapixel camera is another great buy especially if you want to impress. It has a great aesthetic appearance as well as awesome features.

Other companies which make great cameras are Fuji which is known for the Finepix Z10fd which comes with 7.2 megapixels. It has a half inch LCD screen, a great selection of menu options and adjustments and is priced modestly at under $200. It also dabbles as a great gift item to give to teens or loved ones during special occasions.

Kodak, the well-known and much respected camera manufacturer has also thrown its weight behind the pinl digital camera with its release of the pink Kodak EasyShare V1003. This is an impressive gadget well in keeping with the Kodak traditional of quality and innovation. The gadget lives up to its reputation with its 10.6 megapixel power. Even in bad lighting scenarios, the camera performs up to par with the very best.

Buying a pink camera can be tricky nowadays because demand sometimes outstrips supply. But it is still possible to get one online at places like eBay or Ubid. also sells great pink digital cameras.

Digital Architectural Rendering

January 10th, 2017

Digital architectural rendering involves using computer techniques in creating and presenting architectural designs. It is almost like making a 3D film of your house, office, a hotel or some other commercial building. With the advent of computer-aided designs, and the associated technologies, architectural rendering has captured the imagination of all architects and the clients can also give reality to their dream houses.

Digital architectural designing is fascinating, dynamic and extremely flexible. You can use it to incorporate subtle changes in color patterns and designs through various permutations and combinations. This is especially true when you have a particular shade in mind but could not find any other way to express.

Digital architectural rendering involves the skilled work of many technicians, graphic designers, animators and other multimedia experts. Take the case of creating a scene of a swimming pool in your hotel. First the scene is created, say, on the drawing board, photographed and then a movement is infused into it or it is animated. It requires a great amount of computing acumen to create this virtual swimming pool for you. Thereafter, the numerous apparently similar frames with slightly different movements in them are stitched together and made to move fast in seconds to give a convincing virtual movement. These video photographs, taken from different angles, can be manipulated in different ways.

Digital rendering is making great headway by incorporating 3D animated virtual reality. New techniques have been developed to infuse life into the video presentations of the building by integrating them with sound effects and a photo finish. And with 3D glasses, the visual experience can be in a class apart from the rest.