YouTip LogoYouTip

Php Pcre

A regular expression describes a pattern for string matching. It can be used to check if a string contains a certain substring, replace matching substrings, or extract substrings that meet a certain condition. For more content on regular expressions, you can refer to our: (#). In PHP, regular expressions are primarily used through a set of built-in functions, the most common of which are: * `preg_match()`: Searches a string for a matching pattern, returning only the first match. * `preg_match_all()`: Searches a string for a matching pattern, returning all matches. * `preg_replace()`: Searches a string for a matching pattern and performs a replacement. ### preg_match() Function The preg_match() function is used to search a string for a matching pattern, returning only the first match. preg_match(pattern, subject, matches) **Parameter Description:** * pattern: The regular expression pattern. * subject: The string to search. * matches: An optional parameter used to store the match result. ## Example The output of the above code is: Match successful!Array( => Hello) ### preg_match_all() Function The preg_match_all() function is used to search a string for a matching pattern, returning all matches. preg_match_all(pattern, subject, matches) **Parameter Description:** * Used to search a string for a matching pattern, returning all matches. * `pattern`: The regular expression pattern. * `subject`: The string to search. * `matches`: An array used to store all match results. ## Example The output of the above code is: Matched vowels: e, a, a, e, a ### preg_replace() Function The preg_replace() function is used to search a string for a matching pattern and perform a replacement. preg_replace(pattern, replacement, subject) **Parameter Description:** * pattern: The regular expression pattern. * replacement: The replacement string. * subject: The string to search. ## Example The output of the above code is: Replaced string: I love * * * ## PCRE Functions In PHP, we can use the PCRE extension to match string patterns. | Function | Description | | --- | --- | | (#) | Performs a regular expression search and replace | | (#) | Returns array entries that match the pattern | | (#) | Returns the error code from the last PCRE regex execution | | (#) | Performs a global regular expression match | | (#) | Performs a regular expression match | | (#) | Quotes regular expression characters | | (#) | Performs a regular expression search and replace using a callback array | | (#) | Performs a regular expression search and replace using a callback | | (#) | Performs a regular expression search and replace | | (#) | Splits a string by a regular expression | * * * ## PREG Constants | Constant | Description | Since | | --- | --- | --- | | **`PREG_PATTERN_ORDER`** | Orders results so that $matches is a full pattern match, followed by $matches for the first subpattern match, and so on. Only used with preg_match_all(). | since | | **`PREG_SET_ORDER`** | Orders results so that $matches contains the first set of matches, $matches the second set, and so on. Only used with preg_match_all(). | | | **`PREG_OFFSET_CAPTURE`** | See the description for **`PREG_SPLIT_OFFSET_CAPTURE`**. | 4.3.0 | | **`PREG_SPLIT_NO_EMPTY`** | This flag tells preg_split() to return only non-empty pieces. | | | **`PREG_SPLIT_DELIM_CAPTURE`** | This flag tells preg_split() to also capture parenthesized submatch strings. | 4.0.5 | | **`PREG_SPLIT_OFFSET_CAPTURE`** | If this flag is set, for every occurring match the offset of the matching substring will be returned. Note that this changes the return value in an array where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1. This flag can only be used with preg_split(). | 4.3.0 | | **`PREG_NO_ERROR`** | Returned by preg_last_error() when no error has occurred. | 5.2.0 | | **`PREG_INTERNAL_ERROR`** | Returned by preg_last_error() if there was an internal PCRE error. | 5.2.0 | | **`PREG_BACKTRACK_LIMIT_ERROR`** | Returned by preg_last_error() if the backtrack limit was reached. | 5.2.0 | | **`PREG_RECURSION_LIMIT_ERROR`** | Returned by preg_last_error() if the recursion limit was reached. | 5.2.0 | | **`PREG_BAD_UTF8_ERROR`** | Returned by preg_last_error() if the last error was due to malformed UTF-8 data (only applicable in UTF-8 mode regexes). | 5.2.0 | | **`PREG_BAD_UTF8_OFFSET_ERROR`** | Returned by preg_last_error() if the offset did not correspond to a valid UTF-8 code point (only applicable in UTF-8 mode regexes). | 5.3.0 | | **`PCRE_VERSION`** | The PCRE version number and release date (e.g., "_7.0 18-Dec-2006_"). | 5.2.4 |
← Php Preg_Match_AllSvn Revert β†’