SlideShare a Scribd company logo
2
Most read
3
Most read
Programmer Blog http://programmerblog.net
Regular Expressions in PHP
Programmer Blog http://programmerblog.net
Regular 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)
Programmer Blog http://programmerblog.net
Regular 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 p’s.
 p? matches any string containing zero or one p.
 p{2} matches any string containing a sequence of two p’s.
 p{2,3} matches any string containing a sequence of two or three p’s.
Programmer Blog http://programmerblog.net
Regular Expressions
 p{2,} matches any string containing a sequence of at least two p’s.
 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.
 <b>(.*)</b> matches any string enclosed within <b> and </b> (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].
Programmer Blog http://programmerblog.net
Regular 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
 (gif|jpg) 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-Z]{3}|[0-9]{4}) Matches three letters or four numbers
Programmer Blog http://programmerblog.net
Regular 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
Programmer Blog http://programmerblog.net
Regular 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
 PHP’s 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.
Programmer Blog http://programmerblog.net
Regular 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
 <?php
 $line = "Vim is the greatest word processor ever created!";
 if (preg_match("/bVimb/i", $line, $match)) print "Match found!";
 ?>
 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.
Programmer Blog http://programmerblog.net
Regular 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.
 <?php
 $delimitedText = "+Jason+++Gilmore+++++++++++Columbus+++OH";
 $fields = preg_split("/+{1,}/", $delimitedText);
 foreach($fields as $field)
 echo $field."<br />";
 ?>
 OUTPUT:
 Jason
 Gilmore
 Columbus
 OH
Programmer Blog http://programmerblog.net
Regular 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()

More Related Content

What's hot (20)

Regular Expression
Regular Expression
Lambert Lum
 
Andrei's Regex Clinic
Andrei's Regex Clinic
Andrei Zmievski
 
Regular expressions
Regular expressions
Raghu nath
 
Grep
Grep
Dr.M.Karthika parthasarathy
 
Regular expressions and php
Regular expressions and php
David Stockton
 
Adv. python regular expression by Rj
Adv. python regular expression by Rj
Shree M.L.Kakadiya MCA mahila college, Amreli
 
2013 - Andrei Zmievski: Clínica Regex
2013 - Andrei Zmievski: Clínica Regex
PHP Conference Argentina
 
Python advanced 2. regular expression in python
Python advanced 2. regular expression in python
John(Qiang) Zhang
 
Maxbox starter20
Maxbox starter20
Max Kleiner
 
Introduction to Regular Expressions
Introduction to Regular Expressions
Jesse Anderson
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
Prof. Wim Van Criekinge
 
Grep - A powerful search utility
Grep - A powerful search utility
Nirajan Pant
 
15 practical grep command examples in linux
15 practical grep command examples in linux
Teja Bheemanapally
 
Regular Expressions
Regular Expressions
Satya Narayana
 
Java: Regular Expression
Java: Regular Expression
Masudul Haque
 
Looking for Patterns
Looking for Patterns
Keith Wright
 
Introduction to regular expressions
Introduction to regular expressions
Ben Brumfield
 
Bioinformatica p2-p3-introduction
Bioinformatica p2-p3-introduction
Prof. Wim Van Criekinge
 
Python (regular expression)
Python (regular expression)
Chirag Shetty
 
Bioinformatics p2-p3-perl-regexes v2014
Bioinformatics p2-p3-perl-regexes v2014
Prof. Wim Van Criekinge
 
Regular Expression
Regular Expression
Lambert Lum
 
Regular expressions
Regular expressions
Raghu nath
 
Regular expressions and php
Regular expressions and php
David Stockton
 
Python advanced 2. regular expression in python
Python advanced 2. regular expression in python
John(Qiang) Zhang
 
Maxbox starter20
Maxbox starter20
Max Kleiner
 
Introduction to Regular Expressions
Introduction to Regular Expressions
Jesse Anderson
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
Prof. Wim Van Criekinge
 
Grep - A powerful search utility
Grep - A powerful search utility
Nirajan Pant
 
15 practical grep command examples in linux
15 practical grep command examples in linux
Teja Bheemanapally
 
Java: Regular Expression
Java: Regular Expression
Masudul Haque
 
Looking for Patterns
Looking for Patterns
Keith Wright
 
Introduction to regular expressions
Introduction to regular expressions
Ben Brumfield
 
Python (regular expression)
Python (regular expression)
Chirag Shetty
 

Similar to Regular Expressions in PHP, MySQL by programmerblog.net (20)

Regular_Expressions.pptx
Regular_Expressions.pptx
DurgaNayak4
 
3.2 javascript regex
3.2 javascript regex
Jalpesh Vasa
 
Class 5 - PHP Strings
Class 5 - PHP Strings
Ahmed Swilam
 
Regex Basics
Regex Basics
Jeremy Coates
 
Regex posix
Regex posix
sana mateen
 
Regular expressions in oracle
Regular expressions in oracle
Logan Palanisamy
 
Regular expressions
Regular expressions
Raj Gupta
 
Python regular expressions
Python regular expressions
Krishna Nanda
 
PHP Web Programming
PHP Web Programming
Muthuselvam RS
 
Introduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_R
Hellen Gakuruh
 
Unit 1-array,lists and hashes
Unit 1-array,lists and hashes
sana mateen
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
sana mateen
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressions
sana mateen
 
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
Bryan Alejos
 
Php Chapter 4 Training
Php Chapter 4 Training
Chris Chubb
 
Chapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular Expression
azzamhadeel89
 
Regular expressions in Python
Regular expressions in Python
Sujith Kumar
 
Regular expressions
Regular expressions
Nicole Ryan
 
Day5 String python language for btech.pptx
Day5 String python language for btech.pptx
mrsam3062
 
Regular expressionfunction
Regular expressionfunction
ADARSH BHATT
 
Regular_Expressions.pptx
Regular_Expressions.pptx
DurgaNayak4
 
3.2 javascript regex
3.2 javascript regex
Jalpesh Vasa
 
Class 5 - PHP Strings
Class 5 - PHP Strings
Ahmed Swilam
 
Regular expressions in oracle
Regular expressions in oracle
Logan Palanisamy
 
Regular expressions
Regular expressions
Raj Gupta
 
Python regular expressions
Python regular expressions
Krishna Nanda
 
Introduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_R
Hellen Gakuruh
 
Unit 1-array,lists and hashes
Unit 1-array,lists and hashes
sana mateen
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
sana mateen
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressions
sana mateen
 
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
Bryan Alejos
 
Php Chapter 4 Training
Php Chapter 4 Training
Chris Chubb
 
Chapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular Expression
azzamhadeel89
 
Regular expressions in Python
Regular expressions in Python
Sujith Kumar
 
Regular expressions
Regular expressions
Nicole Ryan
 
Day5 String python language for btech.pptx
Day5 String python language for btech.pptx
mrsam3062
 
Regular expressionfunction
Regular expressionfunction
ADARSH BHATT
 
Ad

Recently uploaded (20)

Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
June Patch Tuesday
June Patch Tuesday
Ivanti
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
Muhammad Rizwan Akram
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
Analysis of the changes in the attitude of the news comments caused by knowin...
Analysis of the changes in the attitude of the news comments caused by knowin...
Matsushita Laboratory
 
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
Edge AI and Vision Alliance
 
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
SOFTTECHHUB
 
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
 
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Alliance
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
June Patch Tuesday
June Patch Tuesday
Ivanti
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
Muhammad Rizwan Akram
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
Analysis of the changes in the attitude of the news comments caused by knowin...
Analysis of the changes in the attitude of the news comments caused by knowin...
Matsushita Laboratory
 
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
Edge AI and Vision Alliance
 
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
SOFTTECHHUB
 
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
 
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Alliance
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Ad

Regular Expressions in PHP, MySQL by programmerblog.net

  • 2. Programmer Blog http://programmerblog.net Regular 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.net Regular 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 p’s.  p? matches any string containing zero or one p.  p{2} matches any string containing a sequence of two p’s.  p{2,3} matches any string containing a sequence of two or three p’s.
  • 4. Programmer Blog http://programmerblog.net Regular Expressions  p{2,} matches any string containing a sequence of at least two p’s.  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.  <b>(.*)</b> matches any string enclosed within <b> and </b> (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.net Regular 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  (gif|jpg) 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-Z]{3}|[0-9]{4}) Matches three letters or four numbers
  • 6. Programmer Blog http://programmerblog.net Regular 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.net Regular 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  PHP’s 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.net Regular 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  <?php  $line = "Vim is the greatest word processor ever created!";  if (preg_match("/bVimb/i", $line, $match)) print "Match found!";  ?>  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.net Regular 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.  <?php  $delimitedText = "+Jason+++Gilmore+++++++++++Columbus+++OH";  $fields = preg_split("/+{1,}/", $delimitedText);  foreach($fields as $field)  echo $field."<br />";  ?>  OUTPUT:  Jason  Gilmore  Columbus  OH
  • 10. Programmer Blog http://programmerblog.net Regular 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()