PHP Website Building Guide & Tips - Introduction To Regular Expressions in PHP 
Home of JavaScript Editor Press Releases Submit Press Release JavaScript Editor Free JavaScript Editor
-----------------------
News by categories
   Automotive
   Business
   Computers
   Economy
   Internet
   Lifestyle
   Medical
   Politics
   Religion
   SEO
   Society
   Software
   Sports
   Technology
   Trade
-----------------------




















 


PHP Website Building Guide & Tips - Introduction To Regular Expressions in PHP

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 - rage

Luqman Technologies Pakistan is a leading Search Engine Optimization (SEO Company) based in IT City Lahore Pakistan Asia. A Well Known Website Promotion Company providing Search Engine Marketing, Search Engine Placement and Search Engine Ranking Services, as well as Professional SEO Services, SEO Consultancy, Website Design and Web Development, Domain Name Registration and Website Hosting in Lahore, across Pakistan, and Worldwide to Textile, Telecom, Leather and Industry, Government, Foreign Missions and Embassies, Real Estate Brokers, Agents and Real Estate Companies worldwide. For more information please see our website http://www.luqman-technologies.com

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 - rage. Rage 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 'grip' rely heavily on rage.

Basically rage forms the core in the Linux world. Many scripting languages such as Perl, ruby, pup...etc have built in rage functions as well.

So you can see, learning regular expression is important because they are used allot in many places and probably more so in the future.

Rage 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 rage comes into the picture when writing pup 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 abs, 123, ~@ and so on (some characters are reserved though).

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

Example '[a-z]' will match any alpha character that falls within the 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 'arc' will match 'ace', 'acc', and ‘away’ and so on.

The escape character '' enable interpretation of special characters. Example 'arc' will match 'ac' only. Remember that '.' is a reserved character to represent a wildcard?

Therefore to match a period, i.e. '.', we need to escape it like so '.' The expression [: alum:] 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 [: alum:] might be easier to remember for some people.

The expression [: alpha:] will match all alpha characters. It is a shortcut to [A-Zap-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 [ uncut:] will match all printable characters, excluding spaces and alphanumeric.

The expression [: space:] will match a white space character.

Modifiers

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', 'a', 'asana' 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 'an' only.

A plus sign ('+') matches 1 or more of the preceding term. Example 'a+' will match 'a', 'asana' and so on. It will not match ''.

{Man} matches between m and n occurrences of the preceding term. Example ‘a {1, 3}' will match 'a', 'a' and 'aura' only.

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

Anchors

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 pup starts with $. Try not to confuse with it).

Grouping

Grouping ‘ )' allows modifiers to apply to groups of rage specifies instead of only the immediately proceeding specified. Example ' a | bb)' will match either 'as' or 'bb'

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

PHP Implementation

There are 2 main variants of rage, Perl-compatible rage (PCRE) and POSIX-Extended.

PHP offers quite allot of functions to implement these 2 types of rage.

In PHP, the most commonly used PCRE function is 'pragmatic' and in POSIX-extended rage, 'ere'. Both syntax are slightly different but equally powerful.

The preference to use 'pragmatic' or 'ere' is entirely up to individual although Send suggested that pragmatic is slightly faster.

I prefer to use 'ergo' simply because of my background in Linux administration.

Contact The Author:

Luqman Technologies Pakistan is a Reputed Search Engine Optimization (SEO Company), Providing Total SEO and SEM (Search Engine Marketing) Solutions worldwide with a great focus on Quality Check, Assured, User and Search Engine’s Friendly Website Design, Web Development, We are a Reliable SEO Company you will like to hire for all your SEO needs; Our SEO Services include, Professional SEO Services, Cost Effective SEO Solutions, Affordable and Low Cost Search Engine Optimization, Search Engine Marketing, Search Engine Placement, Submission and Search Engine Advertising, As well as Link Building, Directory Submissions, Content Writing, Development, Article Writing, Press Releases Writing, Development and Distributions. We also provide Cheaper or cheapest Domain Name Registration and Web Site Hosting on Secure, Reliable and Error Free Web hosting Servers for All your Business Ecommerce and Personal Web hosting needs and Our Servers are Up all hours and are based in the United States of America in the neat and clean environment.




Submit Press Release