Regular Expression in PHP

Regular Expression is nothing but a sequence or pattern of characters. They provide the basis for pattern matching.

Regular Expression is nothing but a sequence or pattern of characters. They provide the basis for pattern matching.

Using Regular Expression, you can search for a specific string within another string, you can replace a string by another string and you can divide a string into substring.

PHP provides many specific features for two Regular Expression sets, each corresponding to a specific Regular Expression type. You can use any style according to your preferences.

  1. POSIX Regular Expression
  2. PERL Style Regular Expression

POSIX Regular Expression in PHP

The structure of a POSIX regular expression is not the same as the structure of a characteristic arithmetic expression: diverse elements (operators) are combined to form a more complex expression.

The simplest regular expression is a regular expression that matches a single character, eg q, inside a string like q, quantrimang or quatao.

Below is an explanation for some of the concepts that are being used in POSIX regular expressions. After that, we will introduce you to Regular Expression related to functions.

Square brackets

Square brackets ([]) have special meaning when used in the context of Regular Expressions. They are used to find a sequence of characters.

Expression Description [0-9] Matches any decimal number from 0 to 9 [az] Matches any lowercase character from a to z [AZ] Matches any uppercase character from A to Z [aZ] Matches any character from lowercase a to uppercase Z

The ranges shown above are the common sequence; You can also use the range [0-3] to match any digit from 0 to 3, or the range [bv] to match any lowercase character from b to v.

Quantifier in PHP

The frequency or position of the character sequence with parentheses and single characters may be indicated by a special character. Each special character has a specific meaning. range}, and $ follows a sequence of characters.

Expression Description + Matches any string containing at least one p. p * Match any string containing at least one p. p? Match any string containing 0 or more p. This is an alternative to using p *. p { N } Matches to any string containing a sequence of N p. p {2,3} Match with any string containing 2 or 3 p. p {2,} Matches any string containing at least 2 p. p $ Matches any string with p in its last position. ^ p Match any string with p in its first position.

For example

Here are examples to help you better understand concepts that match characters:

Expression Description [^ a-zA-Z] Matches any string without any characters from a to z and from A to Z. pp Matches to any string containing p, followed by any character anyway, in this case p. ^. {2} $ Matches any string containing exactly 2 characters. (. *) Match any string enclosed inside and . p (hp) * Matches to any string containing a p followed by 0 or more php string representations.

Character strings are predefined in PHP

Make it more convenient for you to program, some predefined character ranges, also known as Character classes, are available to you. Character classes define a whole range of characters, for example, a letter set or an integer set.

Function integrates POSIX Regexp in PHP

Currently, PHP provides seven functions to search strings using POSIX regular expressions. Below is a list of these functions.

  1. Since PHP 5.3.x and above, the functions listed in this table are no longer supported. If you still use it, there will be an error: Deprecated: Function name () is deprecated. Example: Deprecated: Function ereg () is deprecated
  2. If you are using PHP 5.3.x or later, you use the PERL Regexp functions integrated in the table below. Specifically: preg_match () replaces ereg () and eregi (), preg_replace () replaces ereg_replace () and eregi_replace (), preg_split () instead of split ().
Description function

ereg ()

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

ereg_replace ()

The ereg_replace () function looks for the string defined by the pattern and replaces this pattern with the replacement string if found. eregi () The eregi () function finds a string defined by a pattern for a string defined by string. The search is not case sensitive. eregi_replace () The eregi_replace () function works a lot like ereg_replace (), except finding patterns in the string is not case sensitive. split () The split () function divides a string into diverse elements, the limit of each element based on the appearance of the pattern in the string. spliti () The spliti () function works much like the split () function, except that it is not case sensitive. sql_regcase () The function sql_regcase () can be viewed as a utility function, transforming each character in the input parameter string into an expression enclosed by square brackets containing two characters

PERL-style Regular Expression in PHP

PERL-style regular expression is similar to POSIX format. POSIX syntax can almost be used for PERL-style regular expression functions. The truth is, you can use any of the quantifiers introduced above.

Below, we explain some of the concepts used in PERL regular expressions. These will then be functions related to this regular expression type.

Meta characters in PHP

Basically, a meta character is an alphabetic character preceded by a backslash to form a combination of special meaning.

For example, you can search for large amounts of money by using the meta character 'd': / ([d] +) 000 / , where d searches for any string of numbers.

Below is a list of meta characters that can be used in PERL-style regular expressions.

Character Description . a single character s a space character (space, tab, newline) S character is not a space d a digit (0-9) D is not a digit w a word character ( az, AZ, 0-9, _) W is not a character from [aeiou] matching a single character in the set for [^ aeiou] matching a single character outside the given set (foo | bar | baz) matches any given character

Modifier in PHP

Some modifiers are available to make it easier for you to work with Regexp, such as typography, searching in multiple lines, .

Modifier Description i Makes matching action irrespective of case insensitive. m Determine that if the string has newline characters (new lines) or carriage return, the operators ^ and $ will match the newline boundary, instead of matching the string boundary. o Estimate expression only once. s Allowed to use. to match the newline character (new line). x Allows you to use spaces in expression. g Global search (global) for every match. cg Allow continue searching even after global search failed.

PERL function with Regexp integration in PHP

PHP provides the following functions to search strings using PERL regular expression.

Function Descriptionpreg_match () The preg_match () function searches for the string for the pattern, returning true if the pattern exists, otherwise false. preg_match_all () The preg_match_all () function matches all pattern occurrences in the string. preg_replace () The preg_replace () function works like ereg_replace (), except that the regular expression can be used in patterns and alternative input parameters. preg_split () The function preg_split () works like the split () function, except that the regular expression is accepted as input parameters for the pattern. preg_grep () The preg_grep () function searches all elements of input_array, returning all elements that match the regexp pattern. preg_ quote () Quote characters.

Follow tutorialspoint

Previous article: Variable predefined in PHP

Next article: Handling exceptions and errors (Error & Exception Handling)

4 ★ | 1 Vote | 👨 248 Views
« PREV POST
NEXT POST »