SlideShare a Scribd company logo
2
Most read
3
Most read
5
Most read
Introduction PHP and REGEX Jussi Pohjolainen TAMK University of Applied Sciences
Regular Expressions Designed to help working with strings in UNIX environment Easy way to find a pattern in a string and/or replace it if you want Very powerful tool Two types: POSIX and Perl Compatible
Basic Syntax In Regex, one uses patterns Pattern can hold Normal characters Start and end indicators as ^ and $ Count indicators like +, - , ? Logical operators, like | Grouping with {}, (), [] Example (Perl-compatible) /^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/
ereg Functions in PHP ereg-functions uses POSIX regex: int ereg (string pattern, string subject [, array groups]) int eregi (string pattern, string subject [, array groups]) string ereg_replace (string pattern, string replacement, string subject) string eregi_replace (string pattern, string replacement, string subject) array split (string pattern, string subject [, int limit])  array spliti (string pattern, string subject [, int limit])
Example // POSIX REGEX $pattern = "^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$"; $email = "jack@tamk.com"; if ( eregi($pattern, $email) )  print "Match"; else  print "Not match";
Example 2 // POSIX REGEX $pattern = "^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$"; $email = $_GET["e-mail"]; if ( eregi($pattern, $email) )  // Save user input to database else  print "Give proper e-mail address!";
preg function set Perl compatible REGEX: int preg_match  ( string $pattern  , string $subject  [, array &$matches  [, int $flags  [, int $offset  ]]] ) mixed preg_replace  ( mixed $pattern  , mixed $replacement  , mixed $subject  [, int $limit  [, int &$count  ]] ) array preg_split  ( string $pattern  , string $subject  [, int $limit  [, int $flags  ]] )
Example // PERL REGEX $pattern = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/i"; $email = "jack@tamk.com"; if ( preg_match($pattern, $email) )  print "Match"; else  print "Not match";
Perl Regex Examples Basic usage: /pattern/modifiers Examples:  /ha jo/ matches "tur ha jo no" /ha jo/i matches "tur ha JO NO";
Some PERL regex rules REGEX Explanation Example . Any single character /a.c/ matches "abc" or "afc" [ ] Single character that is contained within the brackets /[abc]/ matches "a", or "b" or "c" ^ Find from the beginning of the string /^a/ matches " a ku ankka" $ Find from the end of the string /a$/ matches "aku ankk a " | OR /cat|dog/ matches "dog" or "cat" ^ NOT (when used in middle of pattern) /a^aa/ matches "aba" {n,m} Matches min n, max m /a{1,3}/ matches "a", or "aa", or "aaa" * Matches 0-n /a*/ ? Matches 0-1 /(hello)?/ + Matches 1-n /a+/
Examples /.ala/ ka vala mpi /\. Älä/ Nyt se loppui . Älä  rupea. /^begin/ begin  starts like this. /end$/ this is the  end
Examples /^begin and end$/ begin and end /one|two|three/ this is the  one /[abc]$/ ? /^Hello my name is ^(Jussi)$/i ?
More Perl REGEX Rules REGEX Explanation \w [a-zA-Z0-9_] \W ^ [a-zA-Z0-9_] \n New Line \s Whitespace / Empty \S Not whitespaces / Empty \d Number \D Not Number
Examples /^[+-]?\d+\.?\d*$/ /^\d{1,2}\.\d{1,2}\.?\d{4}?$/ /^(\d{1,2})\:(\d{1,2})\.(\d{1,2})$/ /[CDE]:\\[\w]+\\[\w]+\\/

More Related Content

PDF
Perl Programming - 01 Basic Perl
Danairat Thanabodithammachari
 
PPTX
Asp.net validation
Paneliya Prince
 
PDF
Php array
Nikul Shah
 
PPTX
HTML Fundamentals
BG Java EE Course
 
PPT
Contiguous Memory Allocation.ppt
infomerlin
 
PPTX
Pushdown Automata Theory
Saifur Rahman
 
DOC
Generalized transition graphs
Arham Khan G
 
PDF
Java variable types
Soba Arjun
 
Perl Programming - 01 Basic Perl
Danairat Thanabodithammachari
 
Asp.net validation
Paneliya Prince
 
Php array
Nikul Shah
 
HTML Fundamentals
BG Java EE Course
 
Contiguous Memory Allocation.ppt
infomerlin
 
Pushdown Automata Theory
Saifur Rahman
 
Generalized transition graphs
Arham Khan G
 
Java variable types
Soba Arjun
 

What's hot (20)

PPTX
Http request and http response
Nuha Noor
 
PPT
System call
Sumant Diwakar
 
PDF
Lecture02(constants, variable & data types)
Dhaka University of Engineering & Technology(DUET)
 
PPTX
Analysis of malicious pdf
Raghunath G
 
PPT
PHP complete reference with database concepts for beginners
Mohammed Mushtaq Ahmed
 
PPTX
Intermediate code generator
sanchi29
 
PPSX
Php and MySQL
Tiji Thomas
 
PDF
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
PPTX
4.6 halting problem
Sampath Kumar S
 
PPTX
Longest Common Subsequence (LCS) Algorithm
Darshit Metaliya
 
PPTX
Sgml
rahul kundu
 
PPT
context free language
khush_boo31
 
PPTX
Regular Expression (Regex) Fundamentals
Mesut Günes
 
PPTX
HTML and DHTML
Dr. SURBHI SAROHA
 
PPTX
Servlets
ZainabNoorGul
 
PPTX
Run time administration
Arjun Srivastava
 
PPTX
Program Threats
guestab0ee0
 
PPTX
Translation of expression(copmiler construction)
IrtazaAfzal3
 
PPT
Introduction To Dotnet
SAMIR BHOGAYTA
 
PDF
Query trees
Shefa Idrees
 
Http request and http response
Nuha Noor
 
System call
Sumant Diwakar
 
Lecture02(constants, variable & data types)
Dhaka University of Engineering & Technology(DUET)
 
Analysis of malicious pdf
Raghunath G
 
PHP complete reference with database concepts for beginners
Mohammed Mushtaq Ahmed
 
Intermediate code generator
sanchi29
 
Php and MySQL
Tiji Thomas
 
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
4.6 halting problem
Sampath Kumar S
 
Longest Common Subsequence (LCS) Algorithm
Darshit Metaliya
 
context free language
khush_boo31
 
Regular Expression (Regex) Fundamentals
Mesut Günes
 
HTML and DHTML
Dr. SURBHI SAROHA
 
Servlets
ZainabNoorGul
 
Run time administration
Arjun Srivastava
 
Program Threats
guestab0ee0
 
Translation of expression(copmiler construction)
IrtazaAfzal3
 
Introduction To Dotnet
SAMIR BHOGAYTA
 
Query trees
Shefa Idrees
 
Ad

Similar to PHP Regular Expressions (20)

PPT
Perl Presentation
Sopan Shewale
 
PPT
Bioinformatica 06-10-2011-p2 introduction
Prof. Wim Van Criekinge
 
PPTX
Regular expressions and php
David Stockton
 
PDF
Coffee 'n code: Regexes
Phil Ewels
 
PPTX
Strings,patterns and regular expressions in perl
sana mateen
 
PPTX
Unit 1-strings,patterns and regular expressions
sana mateen
 
PPTX
Javascript正则表达式
ji guang
 
PPT
Php String And Regular Expressions
mussawir20
 
PPT
LPW: Beginners Perl
Dave Cross
 
PDF
Regular Expressions -- SAS and Perl
Mark Tabladillo
 
PPT
CGI With Object Oriented Perl
Bunty Ray
 
PPTX
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Prof. Wim Van Criekinge
 
ODP
Beginning Perl
Dave Cross
 
PDF
Python regular expressions
Krishna Nanda
 
PDF
Maxbox starter20
Max Kleiner
 
PPT
Regular Expressions 2007
Geoffrey Dunn
 
PPT
Class 5 - PHP Strings
Ahmed Swilam
 
ODP
Introduction to Perl - Day 2
Dave Cross
 
XLS
LoteríA Correcta
guest4dfcdf6
 
PDF
MMBJ Shanzhai Culture
MobileMonday Beijing
 
Perl Presentation
Sopan Shewale
 
Bioinformatica 06-10-2011-p2 introduction
Prof. Wim Van Criekinge
 
Regular expressions and php
David Stockton
 
Coffee 'n code: Regexes
Phil Ewels
 
Strings,patterns and regular expressions in perl
sana mateen
 
Unit 1-strings,patterns and regular expressions
sana mateen
 
Javascript正则表达式
ji guang
 
Php String And Regular Expressions
mussawir20
 
LPW: Beginners Perl
Dave Cross
 
Regular Expressions -- SAS and Perl
Mark Tabladillo
 
CGI With Object Oriented Perl
Bunty Ray
 
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Prof. Wim Van Criekinge
 
Beginning Perl
Dave Cross
 
Python regular expressions
Krishna Nanda
 
Maxbox starter20
Max Kleiner
 
Regular Expressions 2007
Geoffrey Dunn
 
Class 5 - PHP Strings
Ahmed Swilam
 
Introduction to Perl - Day 2
Dave Cross
 
LoteríA Correcta
guest4dfcdf6
 
MMBJ Shanzhai Culture
MobileMonday Beijing
 
Ad

More from Jussi Pohjolainen (20)

PDF
Moved to Speakerdeck
Jussi Pohjolainen
 
PDF
Java Web Services
Jussi Pohjolainen
 
PDF
Box2D and libGDX
Jussi Pohjolainen
 
PDF
libGDX: Screens, Fonts and Preferences
Jussi Pohjolainen
 
PDF
libGDX: Tiled Maps
Jussi Pohjolainen
 
PDF
libGDX: User Input and Frame by Frame Animation
Jussi Pohjolainen
 
PDF
Intro to Building Android Games using libGDX
Jussi Pohjolainen
 
PDF
Advanced JavaScript Development
Jussi Pohjolainen
 
PDF
Introduction to JavaScript
Jussi Pohjolainen
 
PDF
Introduction to AngularJS
Jussi Pohjolainen
 
PDF
libGDX: Scene2D
Jussi Pohjolainen
 
PDF
libGDX: Simple Frame Animation
Jussi Pohjolainen
 
PDF
libGDX: Simple Frame Animation
Jussi Pohjolainen
 
PDF
libGDX: User Input
Jussi Pohjolainen
 
PDF
Implementing a Simple Game using libGDX
Jussi Pohjolainen
 
PDF
Building Android games using LibGDX
Jussi Pohjolainen
 
PDF
Android Threading
Jussi Pohjolainen
 
PDF
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Jussi Pohjolainen
 
PDF
Creating Games for Asha - platform
Jussi Pohjolainen
 
PDF
Intro to Asha UI
Jussi Pohjolainen
 
Moved to Speakerdeck
Jussi Pohjolainen
 
Java Web Services
Jussi Pohjolainen
 
Box2D and libGDX
Jussi Pohjolainen
 
libGDX: Screens, Fonts and Preferences
Jussi Pohjolainen
 
libGDX: Tiled Maps
Jussi Pohjolainen
 
libGDX: User Input and Frame by Frame Animation
Jussi Pohjolainen
 
Intro to Building Android Games using libGDX
Jussi Pohjolainen
 
Advanced JavaScript Development
Jussi Pohjolainen
 
Introduction to JavaScript
Jussi Pohjolainen
 
Introduction to AngularJS
Jussi Pohjolainen
 
libGDX: Scene2D
Jussi Pohjolainen
 
libGDX: Simple Frame Animation
Jussi Pohjolainen
 
libGDX: Simple Frame Animation
Jussi Pohjolainen
 
libGDX: User Input
Jussi Pohjolainen
 
Implementing a Simple Game using libGDX
Jussi Pohjolainen
 
Building Android games using LibGDX
Jussi Pohjolainen
 
Android Threading
Jussi Pohjolainen
 
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Jussi Pohjolainen
 
Creating Games for Asha - platform
Jussi Pohjolainen
 
Intro to Asha UI
Jussi Pohjolainen
 

Recently uploaded (20)

PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PDF
Sunset Boulevard Student Revision Booklet
jpinnuck
 
PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPTX
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
PDF
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
Strengthening open access through collaboration: building connections with OP...
Jisc
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PDF
Landforms and landscapes data surprise preview
jpinnuck
 
PDF
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PDF
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
Sunset Boulevard Student Revision Booklet
jpinnuck
 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Strengthening open access through collaboration: building connections with OP...
Jisc
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
Landforms and landscapes data surprise preview
jpinnuck
 
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
Arihant Class 10 All in One Maths full pdf
sajal kumar
 

PHP Regular Expressions

  • 1. Introduction PHP and REGEX Jussi Pohjolainen TAMK University of Applied Sciences
  • 2. Regular Expressions Designed to help working with strings in UNIX environment Easy way to find a pattern in a string and/or replace it if you want Very powerful tool Two types: POSIX and Perl Compatible
  • 3. Basic Syntax In Regex, one uses patterns Pattern can hold Normal characters Start and end indicators as ^ and $ Count indicators like +, - , ? Logical operators, like | Grouping with {}, (), [] Example (Perl-compatible) /^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/
  • 4. ereg Functions in PHP ereg-functions uses POSIX regex: int ereg (string pattern, string subject [, array groups]) int eregi (string pattern, string subject [, array groups]) string ereg_replace (string pattern, string replacement, string subject) string eregi_replace (string pattern, string replacement, string subject) array split (string pattern, string subject [, int limit]) array spliti (string pattern, string subject [, int limit])
  • 5. Example // POSIX REGEX $pattern = "^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$"; $email = "[email protected]"; if ( eregi($pattern, $email) ) print "Match"; else print "Not match";
  • 6. Example 2 // POSIX REGEX $pattern = "^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$"; $email = $_GET["e-mail"]; if ( eregi($pattern, $email) ) // Save user input to database else print "Give proper e-mail address!";
  • 7. preg function set Perl compatible REGEX: int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags [, int $offset ]]] ) mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit [, int &$count ]] ) array preg_split ( string $pattern , string $subject [, int $limit [, int $flags ]] )
  • 8. Example // PERL REGEX $pattern = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/i"; $email = "[email protected]"; if ( preg_match($pattern, $email) ) print "Match"; else print "Not match";
  • 9. Perl Regex Examples Basic usage: /pattern/modifiers Examples: /ha jo/ matches "tur ha jo no" /ha jo/i matches "tur ha JO NO";
  • 10. Some PERL regex rules REGEX Explanation Example . Any single character /a.c/ matches "abc" or "afc" [ ] Single character that is contained within the brackets /[abc]/ matches "a", or "b" or "c" ^ Find from the beginning of the string /^a/ matches " a ku ankka" $ Find from the end of the string /a$/ matches "aku ankk a " | OR /cat|dog/ matches "dog" or "cat" ^ NOT (when used in middle of pattern) /a^aa/ matches "aba" {n,m} Matches min n, max m /a{1,3}/ matches "a", or "aa", or "aaa" * Matches 0-n /a*/ ? Matches 0-1 /(hello)?/ + Matches 1-n /a+/
  • 11. Examples /.ala/ ka vala mpi /\. Älä/ Nyt se loppui . Älä rupea. /^begin/ begin starts like this. /end$/ this is the end
  • 12. Examples /^begin and end$/ begin and end /one|two|three/ this is the one /[abc]$/ ? /^Hello my name is ^(Jussi)$/i ?
  • 13. More Perl REGEX Rules REGEX Explanation \w [a-zA-Z0-9_] \W ^ [a-zA-Z0-9_] \n New Line \s Whitespace / Empty \S Not whitespaces / Empty \d Number \D Not Number
  • 14. Examples /^[+-]?\d+\.?\d*$/ /^\d{1,2}\.\d{1,2}\.?\d{4}?$/ /^(\d{1,2})\:(\d{1,2})\.(\d{1,2})$/ /[CDE]:\\[\w]+\\[\w]+\\/