0% found this document useful (0 votes)
58 views

03.0 PHP Regular Expression

This document discusses regular expressions (regexes) in PHP. It explains that regexes provide a powerful way to work with text by allowing complex validation, parsing, and reformatting of user input and file contents. The document covers the main syntax elements of regexes, including characters, metacharacters, character classes, groups, anchors, and qualifiers. It also compares the POSIX and PCRE implementations in PHP, explaining differences in features and functions for matching and replacing text.

Uploaded by

hey you
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 views

03.0 PHP Regular Expression

This document discusses regular expressions (regexes) in PHP. It explains that regexes provide a powerful way to work with text by allowing complex validation, parsing, and reformatting of user input and file contents. The document covers the main syntax elements of regexes, including characters, metacharacters, character classes, groups, anchors, and qualifiers. It also compares the POSIX and PCRE implementations in PHP, explaining differences in features and functions for matching and replacing text.

Uploaded by

hey you
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/ 20

Programim i Avancuar në Web

1
Regular Expressions in PHP

Master Shkencor Sisteme Informacioni ne Ekonomi 1/25/2021


2 Regular Expressions

 Regular expressions can provide a powerful way to work with text.


 Using regular expressions, you can do complex validation of user input, parse
user input and file contents, and reformat strings.
 Examples- Phone Number That approach wouldn't
 (555) 555-5555 stop users from entering
 555-555-5555 something like:
!555?333-3333
 but not 555-5555
 One solution: Strip out all the non-numerical characters and count to see
whether a total of 10 characters remained.

 This is where regular expressions (regexes) came in  Flexible


solutions

Master Shkencor Sisteme Informacioni ne Ekonomi 1/25/2021


3 Regular Expression

 Regexes can be used to do searches and make replacements when you


have rules to follow, but you don't necessarily have the exact characters
that need to be found or replaced.

 For instance, in the earlier phone number example, the users defined rules
that dictated the format of the phone number entered, but not the digits
contained in the phone number

Master Shkencor Sisteme Informacioni ne Ekonomi 1/25/2021


4 Regular Expression (POSIX vs PCREs)

 PHP supports two implementations of regexes:


 POSIX - Portable Operating System Implementation
 PCREs - Perl-Compatible Regular Expressions (PCREs).
 These implementations offer different features, but they're equally easy to
use in PHP. The regex style you use will depend a lot on your previous
experience with regexes and what you're comfortable using.
 Which to use?
 There is some evidence that PCREs are a little faster than POSIX
expressions.
 Warning for POSIX: This function has been DEPRECATED as of PHP 5.3.0.
Relying on this feature is highly discouraged.

Master Shkencor Sisteme Informacioni ne Ekonomi 1/25/2021


5 Regular Expression Syntax

 POSIX and PCRE - share a syntax (although they differ in their support for
some features and character classes)
 Each regex is made up of one or more:
 Characters
 Special characters (sometimes called metacharacters)
 Character classes
 Groups of characters.
 To look for the literal meaning of special characters (line anchors,
qualifiers), their special meaning should be escaped, using \ (backslash)
 To look for a literal period or dot, use the escape character \: \..

Master Shkencor Sisteme Informacioni ne Ekonomi 1/25/2021


6 Regular Expression Syntax

 Line anchors are special metacharacters that match the beginning or end
of a line, but don't capture any text (see Table 1).
 For example, if a line begins with the letter a, a line anchor in the expression
^a doesn't capture the letter a, but does match the beginning of the line

Master Shkencor Sisteme Informacioni ne Ekonomi 1/25/2021


7 Regular Expression Syntax
 Qualifiers are used to specify how many times an expression can be found
in a search.
 They can be apply to the expressions immediately preceding them (see
Table 2).
 For instance, the expression a+ looks for the letter a found one or more
times.

Master Shkencor Sisteme Informacioni ne Ekonomi 1/25/2021


Regular Expression Syntax
8
 Character classes - defines a set of characters, any one of which can
occur in an input string for a match to succeed.
 POSIX and PCREs have different syntax for Character classes

Master Shkencor Sisteme Informacioni ne Ekonomi 1/25/2021


9 Regular Expression Syntax examples

Master Shkencor Sisteme Informacioni ne Ekonomi 1/25/2021


10 POSIX vs PCREs matches
 Functions to search strings in POSIX and PCRE regexes:

POSIX PCREs
int ereg ( string $pattern , string $string ) int preg_match ( string $pattern , string $string)
- Searches a string for matches to the regular
expression given in pattern in a case-sensitive
way. - Searches string for a match to the
- Returns the length of the matched string if a regular expression given in pattern.
match for pattern was found in string , or FALSE - Returns 1 if the matches given , 0 if
(0) if no matches were found or an error it does not, or FALSE if an error
occurred.
occurred.
int eregi (string $pattern , string $string )
The i modifier, makes the regex
case-insensitive.
- Is similar to ereg(), except that it's case-
insensitive. No need to call a different method

Master Shkencor Sisteme Informacioni ne Ekonomi 1/25/2021


11 POSIX ereg() example
 ereg() example

 The regex [-[:digit:]]{12} looks for 12 characters that are digits or hyphens.
 It's a little sloppy for use with a phone number, and it can instead be written like
this: ^[0-9]{3}-[0-9]{3}-[0-9]{4}$.
 The alternative expression is much more exact. It looks for the beginning of the
line (^), followed by a group of three digits ([0-9]{3}), a hyphen (-), another
group of three digits, another hyphen, a group of four digits, and then the end
of the line ($).
 (In regex, [0-9] and [:digit:] are essentially the same thing; you may prefer [0-9]
because it's shorter.)
Master Shkencor Sisteme Informacioni ne Ekonomi 1/25/2021
12 POSIX eregi() example
 eregi() example

 When you execute this example, it prints Found match! because hello is
found as long as you do the search with case ignored. If you were using
ereg, the match would fail.

Master Shkencor Sisteme Informacioni ne Ekonomi 1/25/2021


13 PCREs preg_match() example
 preg_match() example

 This example uses the regex ^[a-z]+$ to search for any letter a through z found
one or more times ([a-z]+) between the beginning (^) and the end ($) of the
line
 PCRE functions require the expression to begin and end with a delimiter. In most
examples, the delimiter is a single / found at the beginning and end of the
expression, inside the quotes. It's important to remember that this delimiter isn't
part of the expression.
 After the last delimiter in a PCRE, you can add a modifier that changes the
behavior of the regex. The i modifier, for example, makes the regex case-
insensitive.

Master Shkencor Sisteme Informacioni ne Ekonomi 1/25/2021


14 PCRE – delimiters..
 When using the PCRE functions, it is required that the pattern is enclosed by delimiters.
 A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.
 Often used delimiters are forward slashes (/), hash signs (#) and tildes (~).
 The following are all examples of valid delimited patterns.
• /foo bar/
• #^[^0-9]$#
• +php+
• %[a-zA-Z0-9_-]%
 If the delimiter needs to be matched inside the pattern it must be escaped using a
backslash.
 If the delimiter appears often inside the pattern, it is a good idea to choose another
delimiter in order to increase readability.
• /http:\/\//
• #http://#

Master Shkencor Sisteme Informacioni ne Ekonomi 1/25/2021


15 POSIX vs PCREs replacements
 Functions to make replacements in strings in POSIX and PCRE regexes:

POSIX PCREs
string ereg_replace ( string $pattern , mixed preg_replace ( mixed $pattern,
string $replacement, string $string ) mixed $replacement, string $string )
-Scans string for matches to pattern,
-Scans string for matches to pattern, then then replaces the matched text
replaces the matched text with replacement. with replacement.
- The modified string is returned. If no matches
are found in , then it will be returned unchanged - Returns an array if the parameter is
an array, or a string otherwise. If
matches are found, the new string
string eregi_replace ( string $pattern , will be returned, otherwise string will
string $replacement, string $string ) be returned unchanged or NULL if an
error occurred.

- Is similar to ereg_replace(), except that it's The i modifier, makes the regex
case-insensitive. case-insensitive.
No need to call a different method
Master Shkencor Sisteme Informacioni ne Ekonomi 1/25/2021
16 POSIX ereg_replace() example
 ereg_replace() example

 This example demonstrates how to replace an e-mail address in a string


with a hyperlink

Master Shkencor Sisteme Informacioni ne Ekonomi 1/25/2021


17 POSIX eregi_replace() example
 eregi_replace() example

 This example replaces banana with pear, regardless of case.

Master Shkencor Sisteme Informacioni ne Ekonomi 1/25/2021


18 PCREs preg_replace() example
 preg_replace() example

 This example gives a quick demonstration of how to capture parts of text


and use back references, such as \\1. These back references insert
whatever text is matched by the groups in parentheses; in this case, \\1
matches the first group, (\d{3}).
 If the string is (555)5555555, how should you modify the expression?
Answer: ^(?(\d{3}))?(\d{3})(\d{4})$
Look for optional parentheses.
Master Shkencor Sisteme Informacioni ne Ekonomi 1/25/2021
19

Master Shkencor Sisteme Informacioni ne Ekonomi 1/25/2021


20 Online Regular Expression checker
 https://regex101.com/
 http://regexr.com/
 PCRE Functions
• http://www.php.net/manual/en/book.pcre.php
 POSIX Regex Functions
 • http://www.php.net/manual/en/ref.regex.php
 String Functions
• http://www.php.net/manual/en/ref.strings.php

Master Shkencor Sisteme Informacioni ne Ekonomi 1/25/2021

You might also like