Regular Expression in PHP

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

May be interested

  • How to search Google everything with Clipman in LinuxHow to search Google everything with Clipman in Linux
    you can do this by combining the clipman application with a specially formatted url and a simple 'regular expression (regex)' formula '.
  • Germany Set to Host First NFL Regular Season GameGermany Set to Host First NFL Regular Season Game
    as part of its ongoing efforts to expand its global reach, the nfl will see regular-season games take place in germany from 2022 to 2025. the move is the latest in a series of scheduled fixtures that will take place outside the us.
  • Turn your ordinary television into a smart TV with 5 smart waysTurn your ordinary television into a smart TV with 5 smart ways
    instead of spending money on smart tvs, nowadays, families absolutely have the opportunity to experience the top entertainment content when turning regular tvs into smart tvs.
  • Expression usage in PUBG MobileExpression usage in PUBG Mobile
    expression does not help you run better in pubg mobile but will give you more fun moments in the meantime.
  • How to Turn Off a Regular CalculatorHow to Turn Off a Regular Calculator
    can't turn off your calculator? many regular pocket calculators do not have an off button. instead, calculators are designed to automatically shut down after a few minutes of non-use. if you want to turn off the computer immediately, you can use some key combinations.
  • Calculation expressions in Access 2016Calculation expressions in Access 2016
    expressions in ms access can be understood similarly to formulas in excel. expressions can include operators, constants, functions, and identifiers.
  • 5 Important Differences Between Fast Chargers and Regular Chargers5 Important Differences Between Fast Chargers and Regular Chargers
    many people think all chargers are the same, but that assumption leads to hours of slow charging — until they learn about these 6 important differences that are often overlooked.
  • How to Make a Regular Printer Wireless with a Wi Fi RouterHow to Make a Regular Printer Wireless with a Wi Fi Router
    today's tipsmake will show you how to turn a regular printer into a wireless printer by connecting the device to a router. if this doesn't work, you can still connect the printer to a computer connected to the internet and share it with other computers on the network.
  • What is Notepad ++? Compare Notepad ++ and regular NotepadWhat is Notepad ++? Compare Notepad ++ and regular Notepad
    on windows operating systems available for us, notepad software can edit code quickly. but notepad available does not have many features and support for users. in this article, which software tips will guide you about notepad ++? compare notepad ++ and regular notepad.
  • Viber reaches 100 million regular usersViber reaches 100 million regular users
    viber has announced information that its service has reached 100 million regular users. also on this occasion, the company released a new version for computers running windows, os x and linux with many improvements in the logo image (sticker).