Add You
#1 in Business Subscribe Email Print

You are here: Home > Internet and Businesses Online > Web Development > Introduction To Regular Expressions In PHP

Tags

  • between
  • happen
  • submitted
  • uppercase letters
  • numeric digit
  • wildcard therefore

  • Links

  • Standing Up for Home Schooling
  • 2006 Trends in Submarine Design
  • Get More Energy Now!
  • Add You - Introduction To Regular Expressions In PHP

    Mexico Manufacturing Industry
    I see another possible future industry brewing in Mexico, that is the making of mobile homes and coaches. They have year round possibilities and abundant labor. Also they have trade deals with China for inexpensive parts that need to be made for these on the road vehicles. I see a problem for the areas of Indiana, the northern areas and parts of TX where coaches are made. Also in the Carolinas, Virginia and Oregon. If these manufacturing facilities move over the border we will lose another round of jobs. Remember when the appliance businesses and furniture businesses moved over the border, followed by automobile factories. Many Mining industries too. NAFTA creates a place for this and our own labor and facilities are not paying attention to it. We now have another reason, the costs of steel and other
    o 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:

    [[:digit:]]{5}(-[[:digit:]]{4})?

    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

    How to Create a Newsletter that Works - Part 3 (Content)
    The most important component of a newsletter is its content. It is the one element that can either make or break the success of your publication. If you want people to read your newsletter on a continual basis, you must produce interesting, valuable content.Keep in mind the 20/80 rule. Eighty percent of your content must be informative, whereas only 20% should be advertorial in nature. The type of content and its format depend on your readership. Since the newsletter’s purpose is to generate interest in your firm, you should produce content that highlights your skills, and showcases past projects, innovative solutions and awards. You can also build customer loyalty by including client profiles.If it is difficult to come up with content on a regular basis, surf the net and online ezines
    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 alot 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.

    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', '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 occurences of the preceding term. Example 'a{1,3}' will match 'a', 'aa' and 'aaa' only.

    - {n} matches exactly n occurences of the preceding term. Example 'a{2}' will match 'aa' 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 php starts with $. Try not to confuse with it).

    Grouping

    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 alot 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:

    [[:digit:]]{5}(-[[:digit:]]{4})?

    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 '

    Making Lasting Impressions with Business Card Cases
    Buying a gift takes an enormous amount of care and patience. As if that is not bad enough, choosing a gift for your boss or an important client can be nerve-wracking and stressful. Naturally, because you are trying to make a good impression, you would like to come up with a gift that exudes professionalism, attitude, and class, and is, at the same time, unique.One of the things that corporate executives have in common is the business card. With the busy lives they lead, most of them carry numerous business cards in their wallets. This can be cumbersome. Clearly, an excellent present for that executive you badly want to impress is a business card case.Why Business Card Cases?Business card cases provide nifty means for storing and organizing business cards. Business card cases can ea
    xamples 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.

    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', '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 occurences of the preceding term. Example 'a{1,3}' will match 'a', 'aa' and 'aaa' only.

    - {n} matches exactly n occurences of the preceding term. Example 'a{2}' will match 'aa' 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 php starts with $. Try not to confuse with it).

    Grouping

    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 alot 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:

    [[:digit:]]{5}(-[[:digit:]]{4})?

    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

    Is Your CRM (Customer Relationship Management) System Doomed To Fail?
    “Right, People. Let’s blast out that mail campaign we’ve been planning for so long.”It’s time to put your trusty CRM software to work; to let it earn its keep. You run a search of people and companies you want to target.You soon realize something’s wrong when your list is far smaller than anticipated. A quick check reveals many profiles/categories have not been filled in, impacting your search results. Further inspection shows numerous records are incorrect; others are riddled with typos. And that’s just for starters.With a sinking feeling, you realize that last push isn’t going to happen in a hurry.Time for some Damage Control or Preventative Maintenance.Fortunately one of the most common reasons cited for the high failure rate of CRM systems - poor data quality - is also o
    hortcut 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.

    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', '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 occurences of the preceding term. Example 'a{1,3}' will match 'a', 'aa' and 'aaa' only.

    - {n} matches exactly n occurences of the preceding term. Example 'a{2}' will match 'aa' 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 php starts with $. Try not to confuse with it).

    Grouping

    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 alot 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:

    [[:digit:]]{5}(-[[:digit:]]{4})?

    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

    Cold Calling Supplement Guide - Do You Need A Website?
    If you’re still cold calling you may want to add in a few more methods for obtaining clients. One very effective method is using a website. A few good reasons a website is great is because it provides great leverage, it’s fairly cheap, and it has the ability to be seen anywhere around the world.You may be wondering if you need a website if you’re just selling a product or service to a local market. With all the benefits of a website and the low cost, it may be very beneficial for you to have one.For example I know several salespeople who sell door-to-door. You wouldn’t think a salesperson like that would need a website. However, I know that the smart ones have implemented a website into their cold calling activities and it increases their results substantially.One of the reasons that a we
    >

    - {m,n} matches between m and n occurences of the preceding term. Example 'a{1,3}' will match 'a', 'aa' and 'aaa' only.

    - {n} matches exactly n occurences of the preceding term. Example 'a{2}' will match 'aa' 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 php starts with $. Try not to confuse with it).

    Grouping

    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 alot 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:

    [[:digit:]]{5}(-[[:digit:]]{4})?

    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

    Career Planning
    Whether you are about to graduate from college, or want to change jobs at a later point in life, career planning is a very important aspect of the process. Before you actually start applying for positions, it is essential to be certain of the areas you would like to work in. The worst thing that could happen is not to take any steps in the career planning process, and end up in a job that you are unhappy with, or do not have the proper skill set for.Career quizzes are often helpful in the career planning process. These are often a great first step when trying to determine what type of jobs you'd be best suited for. Many are available online, and address some of the following areas: identifying your skills and abilities; determining if you prefer working with people, data or things; or summarizing
    o 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:

    [[:digit:]]{5}(-[[:digit:]]{4})?

    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

    [0-9]{4}(-[0-9]{1,2})+

    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:

    [[:digit:]]{4}(-[[:digit:]]{1,2})+

    or

    [0-9]{4}-[0-9]{1,2}-[0-9]{1,2}

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

    Conclusion

    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 web-developer.sitecritic.net. Good luck.

    HTTP = HTML link (for blogs, profiles,phorums):
    <a href="http://www.addyou.info/article/86916/addyou-Introduction-To-Regular-Expressions-In-PHP.html">Introduction To Regular Expressions In PHP</a>

    BB link (for phorums):
    [url=http://www.addyou.info/article/86916/addyou-Introduction-To-Regular-Expressions-In-PHP.html]Introduction To Regular Expressions In PHP[/url]

    Related Articles:

    Promotional Umbrellas - Quality Counts

    Freelance Writers: How to Turn a Client Meeting Into a Windfall of Work

    PPC Advertising: Where Do You Start?

    Bookmark it: del.icio.us digg.com reddit.com netvouz.com google.com yahoo.com technorati.com furl.net bloglines.com socialdust.com ma.gnolia.com newsvine.com slashdot.org simpy.com shadows.com blinklist.com