Regular Expressions – PHP

Regular expressions are just a set of characters in a certain order. They are the building blocks for pattern-matching features.

Using a regular expression, you can look for a certain string inside of another string, replace one string with another string, or break a string into many smaller pieces.

PHP has two different sets of functions for regular expressions. Each set is for a different kind of regular expression. You can use whichever one is easiest for you.

  1. POSIX Regular Expressions
  2. Regular Expressions in the PERL Style

POSIX Regular Expressions

A POSIX regular expression is similar to an arithmetic expression in that it is made up of different parts (operators) that are combined to make more complex expressions.

The simplest regular expression looks for a single character, like “g,” in strings like “g,” “haggle,” or “bag.”

Let’s talk about a few POSIX regular expression ideas and how they work. After that, we’ll show you some functions that have to do with regular expressions.

When you use brackets ([]) in a regular expression, they mean something different. People use them to find all kinds of characters.

[0-9]

It works with any decimal number from 0 to 9.

[a-z]

It matches any letter from a to z with a lowercase letter.

[A-Z]

It works for any uppercase letter A through uppercase Z.

[a-Z]

It matches any letter from a to Z, lowercase or capital.

The ranges shown above are general. You could also use [0-3] to match any decimal number between 0 and 3, or [b-v] to match any lowercase letter between b and v.

Quantifiers

A special character can show how often a sequence of bracketed characters or a single character appears or where it appears. Each special character means something different. The +, *,?, int. range,” and $ flags all use the same set of characters.

p+

It works with any string that has at least one p in it.

p*

It works for any string with 0 or more p’s.

p?

It works for any string with zero or one p.

p{N}

It matches any string that has N p’s in a row.

p{2,3}

It matches any string that has two or three p’s in a row.

p{2, }

It matches any string that has at least two p’s in a row.

p$

It works with any string that ends in “p.”

^p

It finds strings that start with the letter p.

Examples

The examples below will help you understand how to match characters.

[^a-zA-Z]

It looks for strings that don’t have any of the characters from a to z or from A to Z.

p.p

It works for any string that starts with p, then has any other character, then another p.

^.{2}$

It looks for strings with exactly two characters in them.

<b>(.*)</b>

It looks for any string that is between b> and /b>.

p(hp)*

It works with any string that starts with a p and has zero or more occurrences of the sequence php.

Predefined Character Ranges

Several character ranges, also called “character classes,” are already set up to make programming easier. Character classes describe a whole group of characters, like the alphabet or a set of numbers.

[[:alpha:]]

It works with any string that has the letters aA through zZ.

[[:digit:]]

It works for any string that has the numbers 0 through 9.

[[:alnum:]]

It works with any string that has the letters A through Z and the numbers 0 through 9.

[[:space:]]

It works with any string that has a space in it.

Regexp POSIX Functions in PHP

PHP has seven functions for using POSIX-style regular expressions to search strings.

ereg()

The ereg() function searches a string given by string for a string given by pattern. It returns true if the pattern is found and false otherwise.

ereg replace()

The ereg replace() function looks for the string given by pattern and replaces it with the string given by replacement if it finds it.

eregi()

The eregi() function looks for a string called string anywhere in a string called pattern. Case is not important for the search.

eregi replace()

The eregi replace() function works just like ereg replace(), except that it doesn’t care about case when looking for a pattern in a string.

split(): The split() function will split a string into different parts, with the boundaries of each part depending on how often a pattern appears in the string.

spliti()

The spliti() function works just like its sibling split(), but it doesn’t care about capitalization.

sql regcase()

The sql regcase() function can be thought of as a utility function that changes each character in the input parameter string into a bracketed expression with two characters.

Regular Expressions in the PERL Style

Regular expressions in Perl are similar to the ones in POSIX. The POSIX syntax and the regular expression functions in the Perl style can be used almost the same way. You can actually use any of the quantifiers we talked about in the last POSIX section.

Let’s talk about a few of the ideas that are used in PERL regular expressions. After that, we’ll show you some functions that have to do with regular expressions.

Meta characters

A meta character is just a letter followed by a backslash, which gives the letter and backslash a special meaning.

For example, you can use the ‘d’ meta character to search for large amounts of money: /([d]+)000/. Here, ‘d’ searches for any string of numerical characters.

Here is a list of all the meta characters you can use in PERL Style.

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

Modifiers: There are a number of modifiers that can make working with regexps much easier. For example, you can use case sensitivity, search in more than one line, etc.

Modifier	Description
i 	Makes the match not care about case
m 	Specifies that the and $ operators will now match against a newline boundary instead of a string boundary if the string has a newline or carriage return character.
o 	Only looks at the expression once.
s 	Allows use of . can be used to match a newline character.
x 	You can use white space in the expression to make it clearer
g 	Finds all matches around the Globally 
cg 	Allows a search to go on even if a global match fails.

Regexp in PHP PERL Compatible Functions

PHP has the following functions for searching strings with regular expressions that are compatible with Perl:

preg match()

The preg match() function looks for pattern in string and returns true if pattern is found and false if it is not.

preg match all()

The preg match all() function finds all instances of pattern in string that match pattern.

preg replace()

The preg replace() function works just like ereg replace(), except that regular expressions can be used in the pattern and replacement input parameters.

preg split()

The preg split() function works exactly like the split() function, except that it can take regular expressions as input for pattern.

preg grep()

The preg grep() function looks through all of the items in the input array and returns all of the items that match the regexp pattern.

preg_ quote ()

Quote regular expression characters

People also search

Leave a Comment

Scroll to Top