Getting Started with PHP Regular Expressions

Source: Internet
Author: User
Tags getting started with php modifiers php introduction php regular expression posix uppercase character
[Note:have already pre-ordered your copy of our printed smashing book #3? The book was a professional guide in how to redesign websites and it also introduces a whole new mindset for progressive We b design, written by experts for you.]

1. What is Regular Expressions

The main purpose of regular expressions, also called regex or regexp, is to efficiently search for patterns in a given Tex T. These search patterns is written using a special format which a regular expression parser understands.

Regular expressions is originating from Unix systems, where a program is designed, called grep, to-help the users work with strings and manipulate text. By following a few basic rules, one can create very complex search patterns.

As an example, let's say you ' re given the task to check wether an e-mail or a telephone number has the correct form. Using a few simple commands these problems can easily is solved thanks to regular expressions. The syntax doesn ' t always seems straightforward in first, but once you learn it, and you'll realize that's can do pretty com Plex searches easily, just by typing in a few characters and I ' ll approach problems from a different perspective.

2. Perl Compatible Regular Expressions

PHP has implemented quite a few regex functions which uses different parsing engines. There is major parser in PHP. One called POSIX and the other PCRE or Perl Compatible Regular Expression.

The PHP function prefix for POSIX is ereg_. Since the release of PHP 5.3 This engine was deprecated, but let's has a look at the more optimal and faster PCRE engine.

In PHP every PCRE function starts with preg_ such as Preg_match or preg_replace. You can read the full function list in PHP ' s documentation.

3. Basic Syntax

To use regular expressions first your need to learn the syntax. This syntax consists in a series of letters, numbers, dots, hyphens and special signs, which we can group together using D Ifferent parentheses.

In PHP every regular expression pattern is defined as a string using the Perl format. In Perl, the a regular expression pattern is written between forward slashes, such as/hello/. In PHP this would become a string, '/hello/'.

Now, let's has a look at some operators, the basic building blocks of regular expressions

Operator Description
^ The circumflex symbol marks the beginning of a pattern, although in some cases it can be omitted
$ Same as with the circumflex symbol, the dollar sign marks the end of a search pattern
. The period matches any single character
? It'll match the preceding pattern zero or one times
+ It'll match the preceding pattern one or more times
* It'll match the preceding pattern zero or more times
| Boolean OR
- Matches a range of elements
() Groups a different pattern elements together
[] Matches any single character between the square brackets
{min, max} It's used to match exact character counts
\d Matches any single digit
\d Matches any single non digit caharcter
\w Matches any alpha numeric character including underscore (_)
\w Matches any non alpha numeric character excluding the underscore character
\s Matches whitespace character

As an addition in PHP the forward slash character are escaped using the simple slash \. Example: '/he\/llo/'

To has a brief understanding how these operators is used, let ' s has a look at a few examples:

Example Description
'/hello/' It'll match the word hello
'/^hello/' It would match hello at the start of a string. Possible matches is Hello Orhelloworld, but not Worldhello
'/hello$/' It would match hello at the end of a string.
'/he.o/' It'll match any character between he and O. Possible matches is Heloor heyo, but not hello
'/he?llo/' It'll match either Llo or hello
'/hello+/' It'll match hello on or more time. e.g. hello or Hellohello
'/he*llo/' Matches Llo, hello or Hehello, but not hellooo
'/hello|world/' It'll either match the word hello or world
'/(A-Z)/' Using it with the hyphen character, this pattern would match every uppercase character from a to Z. e.g. a, B, C ...
'/[abc]/' It would match any single character A, B or C
'/abc{1}/' Matches precisely one C character after the characters ab e.g. MATCHESABC, and not ABCC
'/abc{1,}/' Matches one or more C character after the characters ab e.g. Matches Abcor ABCC
'/abc{2,4}/' Matches between and four C character after the characters ab e.g. Matches ABCC, ABCCC or ABCCCC, but not ABC

Besides operators, there is regular expression modifiers, which can globally alter the behavior of search patterns.

The regex modifiers is placed after the pattern, such as this '/hello/i ' and they consists of a single letters such as I which Marks a pattern case insensitive or x which ignores white-space characters. For a full list of modifiers visit PHP ' s online documentation.

The real power of regular expressions relies in combining these operators and modifiers, therefore creating rather complex Search patterns.

4. Using Regex in PHP

In PHP we had a total of nine PCRE functions which we can use. Here's the list:

Preg_filter? Performs a regular expression search and replace Preg_grep? Returns array entries that match a pattern preg_last_error? Returns the error code of the last PCRE regex execution Preg_match? Perform a regular expression match preg_match_all? Perform a global regular expression match preg_quote? Quote Regular expression characters preg_replace? Perform a regular expression search and replace Preg_replace_callback? Perform a regular expression search and replace using a callback preg_split? Split string by a regular expression

The most commonly used functions is preg_match and preg_replace.

Let's begin by creating a test string on which we'll perform our regular expression searches. The classical Hello world should do it.

View plain Copy to clipboard print?

$test _string = ' Hello World ';

If we simply want to search for the word hello or world then the search pattern would look something like this:

View plain Copy to clipboard print?

Preg_match ('/hello/', $test _string); Preg_match ('/world/', $test _string);

If we wish to see if the string begins with the word hello, we would simply put the ^ character in the beginning of the SE Arch pattern like this:

View plain Copy to clipboard print?

Preg_match ('/^hello/', $test _string);

Please note that regular expressions is case sensitive, the above pattern won ' t match the word hElLo. If we want our pattern to is case insensitive we should apply the following modifier:

View plain Copy to clipboard print?

Preg_match ('/^hello/i ', $test _string);

Notice the character I at the end of the pattern after the forward slash.

Now let's examine a more complex search pattern. What if we want to check that the first five characters in the string is Alpha numeric characters.

View plain Copy to clipboard print?

Preg_match ('/^[a-za-z0-9]{5}/', $test _string);

Let's dissect this search pattern. First, by using the caret character (^) we specify, the string must begin with an alpha numeric character. This was specified by [a-za-z0-9].

A-Z means all the characters from A to Z followed by A-Z which are the same except for lowercase character, this is Importa NT, because regular expressions is case sensitive. I Think you'll figure out by yourself what 0-9 means.

{5} Simply tells the regex parser to count exactly five characters. If we put six instead of five, the parser wouldn ' t match anything, because in our test string the word hello is five Chara Cters long, followed by a white-space character which in our case doesn ' t count.

Also, this regular expression could is optimized to the following form:

View plain Copy to clipboard print?

Preg_match ('/^\w{5}/', $test _string);

\w Specifies any alpha numeric characters plus the underscore character (_).

6. Useful Regex Functions

Here is a few PHP functions using regular expressions which you could use on a daily basis.

Validate e-mail. This function would validate a given e-mail address string to see if it has the correct form.

View plain Copy to clipboard print?

function Validate_email ($email _address) {if (!preg_match ("/^ ([a-za-z0-9]) + ([a-za-z0-9\._-]) *@ ([a-za-z0-9_-]) + ([ a-za-z0-9\._-]+) +$/", $email _address)) {return false;} return true; }

Validate a URL

View plain Copy to clipboard print?

function Validate_url ($url) {return Preg_match (' |^http (s)?:/ /[a-z0-9-]+ (. [ a-z0-9-]+) * (: [0-9]+)? (/.*) $|i ', $url); }

Remove repeated words. I often found repeated words in a text, such as this. This handy function would remove such duplicate words.

View plain Copy to clipboard print?

function Remove_duplicate_word ($text) {return preg_replace ("/s (w+s) 1/i", "$", $text);}

Validate Alpha Numeric, dashes, underscores and spaces

View plain Copy to clipboard print?

function Validate_alpha ($text) {return Preg_match ("/^[a-za-z0-9_-]+$/", $text);}

Validate US ZIP Codes

View plain Copy to clipboard print?

function Validate_zip ($zip _code) {return Preg_match ("/^ ([0-9]{5}) (-[0-9]{4}) $/i", $zip _code);}

7. Regex Cheat Sheet

Because cheat Sheets is cool nowadays, below you can find a PCRE cheat sheet so can run through quickly anytime you Forget something.

Meta characters Description
^ Marks the start of a string
$ Marks the end of a string
. Matches any single character
| Boolean OR
() Group elements
[ABC] Item in range (A, B or C)
[^ABC] Not in range (every character except A, B, or C)
\s White-space character
A? Zero or one B characters. Equals to a{0,1}
A * Zero or more of a
A + One or more of a
A{2} Exactly, a
a{,5} Up to five of a
a{5,10} Between five to ten of a
\w Any alpha numeric character plus underscore. Equals to [a-za-z0-9_]
\w Any non alpha numeric characters
\s Any white-space character
\s Any non white-space character
\d any digits. Equals to [0-9]
\d any non digits. Equals to [^0-9]
Pattern Modifiers Description
I Ignore case
M Multiline mode
S Extra Analysis of pattern
U Pattern is treated as UTF-8
8. Useful readings PHP Regular Expression for WEB developers mastering Regular Expressions in PHP Introduction to PHP R Egex

Author:joel Reyes

Joel Reyes have been designing and coding Web sites for several years, this have leads him to being the creative mind behind Loo Ney Designer a design resource and portfolio site that revolves around web and graphic design.

From:http://www.noupe.com/php/php-regular-expressions.html

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.