We now have a youtube channel. Subscribe!

PHP Regular Expressions

PHP Regular Expressions


Hello folks! welcome back to a new section of our tutorial on PHP. In this tutorial guide, we will be studying about the PHP Regular Expressions.

Regular expressions are nothing more than a pattern or series of characters itself. They provide the foundation for pattern-matching functionality.

Using regular expression, you can search a specific string inside of another string, you can replace a string by another string and you can divide a string into many chunks.

PHP gives functions that are specific to two sets of Regular Expression functions, each corresponding to a specific type of regular expression. You can use any of them based on your requirement.

  • POSIX Regular Expressions
  • PERL Style ReExp


POSIX Regular Expressions

POSIX Regular Expressions

The structure of a POSIX regular expression isn't dissimilar to that of a typical arithmetic expression; various elements are merged to form more complex expressions.

The simplest regular expression is the one that matches a single character, such as g, inside the strings, such as g, bottle, or bag.

Lets give explanation for few concepts that are being used in POSIX regular expression. After that we will introduce to you guys the regular expression related functions.

Brackets

Brackets ([]) have a special meaning when used in the context of regular expressions. They are used in PHP program for finding a range of characters.

Sr.NoExpression & Description
1

[0-9]

It matches any decimal digit from 0 through 9.

2

[a-z]

It matches any character from lower-case a through lowercase z.

3

[A-Z]

It matches any character from uppercase A through uppercase Z.

4

[a-Z]

It matches any character from lowercase a through uppercase Z.



The ranges displayed above are all general; you could also use the range [0-5] to match any decimal digit ranging from 0 through 5, or the range [b-v] to match any lowercase character that ranges from b through v.

Quantifiers

The frequency or the position of bracketed character sequences and single characters can be denoted by a special character. With each special character having a particular connotation. +, *, ?, {int.range} and $ flags all follow a character sequence.

Sr.NoExpression & Description
1

p+

It matches any string containing at least one p.

2

p*

It matches any string containing zero or more p's.

3

p?

It matches any string containing zero or one p's.

4

p{N}

It matches any string containing a sequence of N p's

5

p{2,3}

It matches any string containing a sequence of two or three p's.

6

p{2, }

It matches any string containing a sequence of at least two p's.

7

p$

It matches any string with p at the end of it.

8

^p

It matches any string with p at the beginning of it.



Example

The following example below will clear your concepts about the matching characters.

Sr.NoExpression & Description
1

[^a-zA-Z]

It matches any string not containing any of the characters ranging from a through z and A through Z.

2

p.p

It matches any string containing p, followed by any character, in turn followed by another p.

3

^.{2}$

It matches any string containing exactly two characters.

4

<b>(.*)</b>

It matches any string enclosed within <b> and </b>.

5

p(hp)*

It matches any string containing a p followed by zero or more instances of the sequence php.


Predefined Character Ranges

For your programming convenience, several predefined character ranges, also known as character classes, are available. Character ranges are used to specify an entire range of characters, for example, the alphabet or an integer set -

Sr.NoExpression & Description
1

[[:alpha:]]

It matches any string containing alphabetic characters aA through zZ.

2

[[:digit:]]

It matches any string containing numerical digits 0 through 9.

3

[[:alnum:]]

It matches any string containing alphanumeric characters aA through zZ and 0 through 9.

4

[[:space:]]

It matches any string containing a space.



PHP Regexp POSIX Functions

PHP currently provides seven functions for searching strings using POSIX-style regular expressions -

Sr.NoFunction & Description
1ereg()

The ereg() function searches a string specified by string for a string specified by pattern, returning true if the pattern is found, and false otherwise.

2ereg_replace()

The ereg_replace() function searches for string specified by pattern and replaces pattern with replacement if found.

3eregi()

The eregi() function searches throughout a string specified by pattern for a string specified by string. The search is not case sensitive.

4eregi_replace()

The eregi_replace() function operates exactly like ereg_replace(), except that the search for pattern in string is not case sensitive.

5split()

The split() function will divide a string into various elements, the boundaries of each element based on the occurrence of pattern in string.

6spliti()

The spliti() function operates exactly in the same manner as its sibling split(), except that it is not case sensitive.

7sql_regcase()

The sql_regcase() function can be thought of as a utility function, converting each character in the input parameter string into a bracketed expression containing two characters.


We are going to be discussing about the above listed functions in our subsequent tutorials.


PERL Style Regular Expressions

PERL Style RegExp

The perl-style regular expression are similar to their POSIX peer. You can use the POSIX syntax almost interchangeably with the Perl style regular expression functions. In fact, you can use any of the quantifiers found in the previous POSIX section.

Lets give explanation for few concepts that are used in PERL regular expressions. After that we are going to introduce you with the regular expression related functions.

Meta Characters

A meta character is simply an alphabetical character preceded by a backslash that act to give the combination a special meaning.

For example, you can look for large money sums making use of the '\d' meta character : /([\d]+)000/. So here \d will search for any string of numeric character.

The following is the list of meta characters that can be used in PERL Style Regexp.

Character		Description
.                            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)      matches any of the alternatives specified


Modifiers

Many modifiers are available that can make your work with regexp so much simpler, like case sensitivity, searching in multiple lines etc.

Modifier	Description
i 	Makes the match case insensitive
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
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

PHP Regexp PERL Functions

PHP provides the following functions below for searching strings using perl-compatible regular expressions -

Sr.NoFunction & Description
1preg_match()

The preg_match() function searches string for pattern, returning true if pattern exists, and false otherwise.

2preg_match_all()

The preg_match_all() function matches all occurrences of pattern in string.

3preg_replace()

The preg_replace() function operates just like ereg_replace(), except that regular expressions can be used in the pattern and replacement input parameters.

4preg_split()

The preg_split() function operates exactly like split(), except that regular expressions are accepted as input parameters for pattern.

5preg_grep()

The preg_grep() function searches all elements of input_array, returning all elements matching the regexp pattern.

6preg_ quote()

Quote regular expression characters


We are going to discuss about the above listed functions in our subsequent tutorials.


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we will be discussing about the PHP ereg() Function.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain