Regular expressions are used for complex string manipulation in PHP.A regular
expression is a pattern that is matched against a subject string from left to right.
To interpret data you need regular expressions. In PHP regular expessions are
accessed through functions.
ereg()
ereg_replace()
eregi()
eregi_replace()
split()
spliti()
sql_regcase()
Meta Characters
===============
Part of a pattern that is in square brackets is called a "character class". In a
character class the only meta- characters are:
\ general escape character
^ negate the class, but only if the first character
- indicates character range
] terminates the character class
Outside square brackets, the meta-characters are as follows:
\ general escape character
^ start of line
$ end of line
. match any character except newline (by default)
[ start character class definition
] end character class definition
| start of alternative branch
( start subpattern
) end subpattern
? extends the meaning of (, also 0 or 1 quantifier, also quantifier minimizer
* 0 or more quantifier
+ 1 or more quantifier
{ start min/max quantifier
} end min/max quantifier
It is always safe to precede a non-alphameric with "\". It can also be used with non
printable chracters. Like ...
/a alarm, /cchar control + char where char is the character
/e escape,/f form feed, /n newline, /r carrige return
/t tab
Backslash can also be used for specifying generic charac- ter types:
\d - any decimal digit
\D - any character that is not decimal digit.
\s - any whitespace character
\S - any character that is not white space.
\w - any word character
\W - any non word character.
Matching Strings
================
While matching strings two meta-character are widely used $(dollar sign) and ^
(circumflex) sign. $ sign tells PHP to match the string at the end of the string,
the ^sign will match any pattren at the beginning of the string.
Expression ^liquidpulse would match the following ..
liquidpulse is one of the best web resource
liquidpulse is best php resource
Expression resource$ will also match the above lines.
Matching Escape Sequences
=========================
Consider expression jerrett\ntaylor. Criteria (jerret, linebreak,taylor) would match
followings
liquidpulse
taylor is lastname of Jerrett
Character Classes
=================
[a-zA-z] Match any letter
[a-zA-Z0-9] Match any letter or number
[ \t\n\v\r\f] Match any whitespace character
[0-9\.\-] Match any number, minus sign or period
To negate the character class, you can use the ^ meta character but only when ^ is
the very first character inside the brackets like [^a-z]
More then one Occurance of a Character
Usee {} metacharacter
[A-Z]{5} Number 5 tells PHP to match a string that contains five uppercase
characters in
a row.To match a range of characters specify lower and upper limits like {1,6}
The * quantifier will match zero or more occurrences of a character and the +
quantiier
will match one or more occurences of a character. [a-z]* will match zero or more
occurences of lowercase letters in a string.
Optional Matches
================
? meta-character is used for this purpose
[a-z]{4}? would optionally match four occurences of any lowercase characters so
following would be valid.
NETRPX
jerrett taylor
LP
Predefined character Classes
============================
[[:alnum:]] All alphanumeric characters [a-zA-Z09]
[[:alpha:]] All alphabatic characters [a-Z]
[[:blank:]] Tab and Space
[[:cntrl:]] All the control characters
[[:digit:]] All decimal digits [0-9]
[[:graph:]] All printable characters except space
[[:lower:]] All lowecase characters
[[:print:]] All printable characters
[[:punct:]] Punctuation marks [\.,;:-]
[[:space:]] All whitespace characters
[[:upper:]] All the upercase letters [A-Z]
[[:xdigit:]] The set of hexadecimal digits
To negate the class make sure that you put ^ inside the first bracket.
[^[:digit:]]
The pipe (|) Delimiter
Here is an example (liquidpulse|netrpx) , PHP will match either liquidpulse or
netrpx.