0% found this document useful (0 votes)
58 views14 pages

PHP - Regular Expressions

Regular expressions are patterns used to match character combinations in strings. They are supported in PHP through functions like preg_match() and preg_replace(). Regular expressions use special characters like brackets, quantifiers, and character classes to match strings. They can be used to validate data like emails and perform search and replace operations on strings.

Uploaded by

A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views14 pages

PHP - Regular Expressions

Regular expressions are patterns used to match character combinations in strings. They are supported in PHP through functions like preg_match() and preg_replace(). Regular expressions use special characters like brackets, quantifiers, and character classes to match strings. They can be used to validate data like emails and perform search and replace operations on strings.

Uploaded by

A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

PHP - Regular

Expressions
Regular
• Regular expressions
expressions are sequence or
pattern of characters
• It provides pattern-matching functionality
• Used to search a particular string
inside a another string
• Validate input such as email address,
domain names, telephone numbers, IP
addresses and so on
• Regular expression functions supported by
PHP
– PERL Style Regular Expressions
– POSIX Regular Expressions
PERL Compatible
Functions
• preg_match
– It is used to perform a pattern match on a string.
– It returns true if a match is found and false if a
match is not found.
• preg_split
– It is used to perform a pattern match on a
string and then split the results into a numeric
array
• preg_replace
– It is used to perform a pattern match on a string
and then replace the match with the specified
Synta
x
<?php
function_name('/pattern/', source);
?>
• /…/ The forward slashes denote the
beginning and end of our regular
expression
• ‘/pattern/’ is the pattern that we need
to matched
• Source is the text string to be matched
against
Brackets
[] used to find a
– Brackets ([]) are
range of characters.
– [0-9] : It matches any decimal digit from 0
through 9.
– [a-z]: It matches any character from
lower-case a through lowercase z.
– [A-Z]: It matches any character from
uppercase A through uppercase Z.
– [a-Z]: It matches any character from
lowercase a through uppercase Z.
Quantifie
rs of bracketed
• Frequency or position
character sequences and single characters
• can be denoted
. (dot)
– Matches any single character except a new line
• p+
– It matches any string containing at least one
p.
• p*
– It matches any string containing zero or
more p's.
• p?
Quantifiers
• $
(Contd..)
– It matches any string at end of it.
• ^
– It matches any string from the beginning of it
• p{N}
– It matches any string containing a sequence of N p's
• p{2,3}
– It matches any string containing a sequence of
two or three p's.
• p{2, }
– It matches any string containing a sequence of at
least two p's.
Exampl
• [^a-zA-Z] es
– It matches any string not containing any of the
characters ranging from a through z and A through
Z.
• p.p
– It matches any string containing p, followed by
any character, in turn followed by another p.
• ^.{2}$
– It matches any string containing exactly two
characters.
• <b>(.*)</b>
– It matches any string enclosed within <b> and </b>.
• p(hp)*
– It matches any string containing a p followed by
Predefined Character
Ranges
• [[:alpha:]]
– It matches any string containing alphabetic
characters aA through zZ.
• [[:digit:]]
– It matches any string containing numerical
digits 0 through 9.
• [[:alnum:]]
– It matches any string containing
alphanumeric characters aA through zZ
and 0 through 9.
• [[:space:]]
– It matches any string containing a space.
PERL Style Regular
Expressions
• PCRE (Perl Compatible Regular
Expressions) library which is enabled
in almost all PHP installations.
Meta

characters
Meta character is simply an alphabetical
character preceded by a backslash
• .
– a single character
• \s
– a whitespace character (space, tab, newline)
• \S
– non-whitespace character
• \d
– a digit (0-9)
• \D
– a non-digit
• \w
– a word character (a-z, A-Z, 0-9, _)
• \W
– a non-word character
• [aeiou]
– matches a single character in the given set
• [^aeiou]
– matches a single character outside the
given set
• (foo|bar|baz)
Modifier
•i
s
– Makes the match case insensitive
•o
– Evaluates the expression only once
•s
– Allows use of . to match a newline
character
•x
– Allows you to use white space in the
expression for clarity
• g
– Globally finds all matches
• cg
– Allows a search to continue even after a global
match fails
• m
– Specifies that if the string has newline or
carriage return characters, the ^ and $
operators will now match against a newline
boundary, instead of a string boundary

You might also like