PHP Regular Expressions

PHPz
Release: 2024-08-29 13:01:52
Original
550 people have browsed it

Regular expression can be defined as pattern matching algorithm generated in a single line. These are impactful in case of validation checking and for template recognitions. The meta characters enables user to handle complex patterns. Thus the support in PHP for regular expression helps to enhance code quality of a PHP programming. Any regular expression is generic pattern or sequence of a set of characters which is used to provide pattern matching functionality against a given subject string. It is also known as RegExp or RegEx. It is also considered as pattern notation based small programming language to be used for text string parsing.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

2 Sets of Regular Expressions in PHP Functions

Given below supports 2 sets of regular expressions:

  • POSIX Regular Expressions
  • PERL Style Regular Expressions

1. POSIX Regular Expressions

This is defined as set of character where any of its single character needs to match from the input string. These expressions are defined within [].

Example:

  • [0-9]: This is designed to filter any decimal string from 0 through 9.
  • [a-Z]: This is designed to filter any character from lowercase ‘a’ through uppercase ‘Z’.

In order to make filter to be more specific, a standard syntax, including regex and special characters is developed which are known as Quantifiers. It also provides information regarding frequency i.e. the number of occurrence or instances of bracketed character or a group of characters and quantity.

Table to find out descriptions for different quantifiers:

Quantifier Description
S+ Filters out a string having at least one ‘s’.
S* Filters out a string having zero or more ‘s’.
S? Filters out a string having zero or one ‘s’.
S{N} Filters out a string having a sequence of N ‘s’.
S$ Filters out a string having ‘s’ at the end.
^S Filters out a string having ‘s’ at the beginning.
Quantifier

Description

S+ Filters out a string having at least one ‘s’.
S* Filters out a string having zero or more ‘s’.
S? Filters out a string having zero or one ‘s’.
S{N} Filters out a string having a sequence of N ‘s’.
S$ Filters out a string having ‘s’ at the end.
^S Filters out a string having ‘s’ at the beginning.
PHP also supports matching functionality with respect to predefined character ranges/classes.
predefined character class Description
[[:space:]] Filters out a string having a space.
[[:alpha:]] Filters out a string having alphabetic characters a-A through z-Z.
[[:digit:]] Filters out a string having numerical 0 to 9.
[[:alnum:]] Filters out a string having alphanumeric characters a-A through z-Z and numerical 0 to 9.
Example:
predefined character class Description
[[:space:]] Filters out a string having a space.
[[:alpha:]] Filters out a string having alphabetic characters a-A through z-Z.
[[:digit:]] Filters out a string having numerical 0 to 9.
[[:alnum:]] Filters out a string having alphanumeric characters a-A through z-Z and numerical 0 to 9.

For the POSIX regex,PHP incorporates various functions to carry out various operation using POSIX-style Regexes.

The functions are described as shown in the below table:

POSIX Regex function Description
ereg() Used to search a string specified by or by pattern and to return true if the matching is found.
ereg_replace() Used to search a string specified by or by pattern and replace with replacement if the matching is found.
eregi() Used to perform non-case sensitive search for a string specified by or by pattern and to return true if the matching is found.
eregi_replace() Used to perform non-case sensitive for a string specified by or by pattern and replace with replacement if the matching is found.
split() Used to divide the string into separate elements based on boundaries that matches the searching pattern.
spliti() Used to perform non-case sensitive for the string to divide it into separate elements based on boundaries that matches the searching pattern.
sql_regcase() A utility function that convert each character from the input value into a bracketed expression making two characters.

2. PERL Style Regular Expressions

This type of Regex patterns similar to POSIX regex but created with meta characters and identifiers. Syntax for this Regexes are interchangeable with POSIX style.

a. Meta characters: An alphabet character preceded by a backslash representing a specific meaning is known as a Meta character.

There are various meta characters that are supported in PHP scripting, being used as Perl type Regex as discussed below:

Meta character Description
. Single character
d A digit character
D Non-digit character
s white space character e.g. SPACE, NEW LINE, TAB
S Non- white space character
w A word character
W Non-word character
[aeiou] Filters the matched character out of the given set
[^aeiou] Filters the unmatched character out of the given set
(set1|set2|set3) Filters the matched element that matches to any of the given alternatives
Meta character

Description
. Single character
d A digit character
D Non-digit character
s white space character e.g. SPACE, NEW LINE, TAB
S Non- white space character
w A word character
W Non-word character
[aeiou] Filters the matched character out of the given set
[^aeiou] Filters the unmatched character out of the given set
(set1|set2|set3) Filters the matched element that matches to any of the given alternatives

b. Modifiers:

These elements enable user to avail additional flexibility to work with regexp.
Modifier Description
g Finds matchings globally.
cg Enable continue global search even after matching fails.
i Instructs to perform case insensitive search.
s Use the character ‘.’ to search for new line character.
m In case of input string containing new line or carriage return character, ‘^’ and ‘$’ are used to match for new line boundary.
x Permits to use white space to improve the clarity of the expression.
o Restrict the evaluation of the expression to occur only once.
Various modifiers and their functionalities are mentioned in the table given below:
Modifier Description
g Finds matchings globally.
cg Enable continue global search even after matching fails.
i Instructs to perform case insensitive search.
s Use the character ‘.’ to search for new line character.
m In case of input string containing new line or carriage return character, ‘^’ and ‘$’ are used to match for new line boundary.
x Permits to use white space to improve the clarity of the expression.
o Restrict the evaluation of the expression to occur only once.

Similar to POSIX regex function, PHP also offers some specific functions that are compatible with PERL style regex.

Some of the major functions are discussed as below:

PERL style regexpcompitable function Description
preg_match() Return the first occurrence of the matching pattern.
preg_match_all() Return all occurrences of the matching pattern.
preg_split() Splits the string input into several elements based on the given regexp pattern as input.
Preg_quote() Used to quote the characters of the regex.
preg_grep() Used to find all the matching elements from array input.
preg_replace() Used to find matching element and replace it with the given replacement.

Example of PHP Regular Expressions

The below example demonstrates the application

The code snippet is designed to scan through the input string and split the given input into multiple elements by defining the given Regex as boundary.

Code:

<?php
// Declaring a regex
$regex = "([0-9]+)";
// Defining the input string
$inputstr = "String_a 1 String_b 2 String_c 3";
//Splitting the input string based on matching regex expression
$result = preg_split ($regex, $inputstr);
// Displaying result
echo $result[0];
echo "\n";
echo $result[1];
echo "\n";
echo $result[2];
echo "\n";
?>
Copy after login

Output

The function preg_split() has split the input string into 3 parts as the elements ‘1’, ‘2, and ‘3’ are marked as boundaries.

PHP Regular Expressions

The above is the detailed content of PHP Regular Expressions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template