PHP regular expressions - PowerPoint PPT Presentation

About This Presentation
Title:

PHP regular expressions

Description:

Regular expressions provide the foundation for describing or matching data according to defined syntax rules. A regular expression is nothing more than a pattern of characters itself, matched against a certain parcel of text. – PowerPoint PPT presentation

Number of Views:145
Slides: 11
Provided by: Username withheld or not provided
Tags: php

less

Transcript and Presenter's Notes

Title: PHP regular expressions


1
pHP-MySQL Web Development
Programmer Blog
http//programmerblog.net
  • Regular Expressions in PHP

2
Programmer Blog

http//programmerblog.netRegular Expressions in
PHP
  • Regular Expressions
  • Regular expressions provide the foundation for
    describing or matching data according to defined
    syntax rules. A regular expression is nothing
    more than a pattern of characters itself, matched
    against a certain parcel of text.
  • PHP offers functions specific to two sets of
    regular expression functions, each corresponding
  • to a certain type of regular expression
    POSIX and Perl-style.
  • Regular Expression Syntax (POSIX)

3
Programmer Blog
http//programmerblog.netRegular
Expressions in PHP
  • Quantifiers
  • The frequency or position of bracketed character
    sequences and single characters can be denoted by
    a special character, with each special character
    having a specific connotation. The , , ?,
    occurrence_range
  • The frequency or position of bracketed character
    sequences and single characters can be denoted by
    a special character, with each special character
    having a specific connotation. The , , ?,
    occurrence_range , and
  • Brackets
  • Brackets () have a special meaning when used in
    the context of regular expressions, which are
    used to find a range of characters.
  • The regular expression php will find any string
    containing the character p or h
  • 0-9 matches any decimal digit from 0
    through 9.
  • a-z matches any character from
    lowercase a through lowercase z.
  • A-Z matches any character from uppercase
    A through uppercase Z.
  • A-Za-z matches any character from uppercase A
    through lowercase z.
  • p matches any string containing at least one p.
  • p matches any string containing zero or more
    ps.
  • p? matches any string containing zero or one p.
  • p2 matches any string containing a sequence of
    two ps.
  • p2,3 matches any string containing a sequence
    of two or three ps.

4
Programmer Blog
http//programmerblog.netRegular
Expressions
  • p2, matches any string containing a sequence
    of at least two ps.
  • p matches any string with p at the end of it.
  • Still other flags can precede and be inserted
    before and within a character sequence
  • p matches any string with p at the beginning of
    it.
  • a-zA-Z matches any string not containing any
    of the characters ranging from a
  • through z and A through Z.
  • p.p matches any string containing p, followed by
    any character, in turn followed by
  • another p.
  • .2 matches any string containing
    exactly two characters.
  • ltbgt(.)lt/bgt matches any string enclosed
    within ltbgt and lt/bgt (presumably HTML bold tags).
  • p(hp) matches any string containing
    a p followed by zero or more instances of the
    sequence hp.
  • Search for these special characters in strings
  • The characters must be escaped with a backslash
    (\). For example, if you wanted to search for a
    dollar amount, a plausible regular expression
    would be as follows (\)(0-9) that is, a
    dollar sign followed by one or more integers.
  • 42, 560, and 3.
  • Predefined Character Ranges (Character Classes)
  • For your programming convenience, several
    predefined character ranges, also known as
    character
  • classes, are available.
  • alpha Lowercase and uppercase alphabetical
    characters. This can also be specified as
    A-Za-z.

5
Programmer Blog
http//programmerblog.netRegular
Expressions
  • Regular Expression Will match...
  • foo The string "foo"
  • foo "foo" at the start of
    a string
  • foo "foo" at the end of a
    string
  • foo "foo" when it is
    alone on a string
  • abc a, b, or c
  • a-z Any lowercase letter
  • A-Z Any character that
    is not a uppercase letter
  • (gifjpg) Matches either "gif" or "jpeg"
  • a-z One or more
    lowercase letters
  • 0-9\.\- ?ny number, dot, or minus sign
  • a-zA-Z0-9_1, Any word of at least one
    letter, number or _
  • (wx)(yz) wy, wz, xy, or xz
  • A-Za-z0-9 Any symbol (not a number or a
    letter)
  • (A-Z30-94) Matches three letters or
    four numbers

6
Programmer Blog
http//programmerblog.netRegular
Expressions
  • Regular Expression Syntax (Perl Style)
  • The developers of PHP felt that instead of
    reinventing the regular expression wheel, so to
    speak, they should make the famed Perl regular
    expression syntax available to PHP users, thus
    the Perl-style functions.
  • . Match any character
  • ˆ Match the start of the string
  • Match the end of the string
  • \s Match any whitespace character
  • \d Match any digit
  • \w Match any word character
  • \A Matches only at the beginning of the
    string.
  • \b Matches a word boundary.
  • \B Matches anything but a word boundary.
  • A caret () character at the beginning of a
    regular expression indicates that it must match
    the beginning of the string.
  • The dot (.) metacharacter matches any single
    character except newline (\). So, the pattern h.t
    matches hat, hothit, hut, h7t, etc.
  • The vertical pipe () metacharacter is used for
    alternatives in a regular expression. It behaves
    much like a logical OR operator

7
Programmer Blog
http//programmerblog.netRegular
Expressions
  • The metacharacters , , ?, and affect the
    number of times a pattern should be matched.
  • means "Match one or more of the preceding
    expression", means "Match zero or more of the
    preceding expression",
  • ? means "Match zero or one of the preceding
    expression".
  • Curly braces can be used differently. With a
    single integer,
  • n means "match exactly n occurrences of the
    preceding expression", with one integer and a
    comma, n, means "match n or more occurrences of
    the preceding expression", and with two
    comma-separated integers n,m means "match the
    previous character if it occurs at least n times,
    but no more than m times".
  • /food/
  • Notice that the string food is enclosed between
    two forward slashes. Just like with POSIX regular
    expressions, you can build a more complex string
    through the use of quantifiers
  • /fo/
  • This will match fo followed by one or more
    characters. Some potential matches include
  • food, fool, and fo4
  • PHPs Regular Expression Functions (Perl
    Compatible)
  • preg_grep()
  • array preg_grep (string pattern, array input ,
    flags)
  • The preg_grep() function searches all elements of
    the array input, returning an array consisting of
  • all elements matching pattern.

8
Programmer Blog
http//programmerblog.netRegular
Expressions
  • foods array("pasta", "steak", "fish",
    "potatoes")
  • food preg_grep("/p/", foods)
  • print_r(food)
  • preg_match()
  • int preg_match (string pattern, string string ,
    array matches
  • , int flags , int offset)
  • The preg_match() function searches string for
    pattern, returning TRUE if it exists and FALSE
  • otherwise. The optional input parameter
    pattern_array can contain various sections of the
  • subpatterns contained in the search pattern
  • lt?php
  • line "Vim is the greatest word processor ever
    created!"
  • if (preg_match("/\bVim\b/i", line, match))
    print "Match found!"
  • ?gt
  • preg_match_all()
  • int preg_match_all (string pattern, string
    string, array pattern_array
  • , int order)
  • The preg_match_all() function matches all
    occurrences of pattern in string, assigning each
    occurrence to array pattern_array in the order
    you specify via the optional input parameter
    order.

9
Programmer Blog
http//programmerblog.netRegular
Expressions
  • preg_replace_callback()
  • mixed preg_replace_callback(mixed pattern,
    callback callback, mixed str
  • , int limit)
  • Rather than handling the replacement procedure
    itself, the preg_replace_callback() function
    delegates the string-replacement procedure to
    some other user-defined function.
  • preg_split()
  • array preg_split (string pattern, string string
    , int limit , int flags)
  • The preg_split() function operates like pattern
    can also be defined in terms of a regular
    expression.
  • lt?php
  • delimitedText "JasonGilmoreColum
    busOH"
  • fields preg_split("/\1,/", delimitedText)
  • foreach(fields as field)
  • echo field."ltbr /gt"
  • ?gt
  • OUTPUT
  • Jason
  • Gilmore
  • Columbus
  • OH

10
Programmer Blog
http//programmerblog.netRegular
Expressions
  • ereg(p,str)
  • ergi(p,str)
  • ereg_replace(p,r, str)
  • sql_regcase(p)
  • split(p,str)
  • preg_match()
  • pre_match_all()
  • preg_replace()
  • preg_split()
Write a Comment
User Comments (0)
About PowerShow.com