|
|
|
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.
|
|
| PHP Script to find url links in a page Categories : PHP, URLs, Regexps, Arrays | | | Tag content retrieval from websites with preg_match Categories : PHP, Regexps, Arrays, HTML and PHP | | | This script is a contact form between users of a
website (kinda like the PM function on the forums)
Categories : PHP, Databases, MySQL, Regexps | | | Simple way to replace a variable value in a .conf (.ini) file using a
webbrowser - the first stage of a complete universal configuration editor Categories : PHP, Regexps, Code Editors, Filesystem | | | ereg -- Regular expression match Categories : PHP, PHP Functions, Regexps | | | Gets the browser and OS from the $_SERVER['http_user_agent'] variable in PHP Categories : PHP, HTTP, Regexps | | | I need a trim function/regexp that will trim all " " from the ends of a string. Categories : Regexps, PHP, Strings | | | Check for functional file links (broken Files)
Categories : PHP, Data Validation, FTP, Regexps, Arrays | | | Form input return conformance Categories : HTML and PHP, PHP, Regexps | | | Simple PHP program which calls other PHP program you can pass the
variables to other PHP program : by Raju Categories : PHP, PHP Options and Info, Regexps, Program Execution | | | How to build a search query for any N number of words in a search string Categories : PHP, Regexps, Search Engines, Search | | | Link Extractor - This function is used to extract links from a given URL. This will convert relative path into absolute path and also remove PHPSESSID stuff. Categories : PHP, URLs, Regexps | | | Newbie Notes #7 - Ridiculous regex Categories : PHP, Beginner Guides, Regexps | | | email validator check checker email e-mail email address Categories : PHP, Email, Regexps | | | An array of functions to use in checking user input to HTML forms : text, firstName, middleNameOrInit, lastName, email, web, digits, decimal, hex, genNum, USD, BPS, Euro, USphone, USzip Categories : PHP, Data Validation, Regexps | |
|
|
|