Log in / create account | Login with OpenID
DocForge
Programmer's Wiki

PHP/Regular expressions

From DocForge

< PHP

Perl-compatible regular expressions are available natively in PHP. The "ereg" library of regular expressions has been deprecated.

Perl-like regular expressions are implemented in preg_ functions.

Contents

[edit] preg_grep

PHP >= 4

array preg_grep ( string $pattern, array $input [, int $flags] )

Returns an array consisting of the elements of the input array that match the given pattern. The returned array is indexed using the keys from the input array.

[edit] preg_last_error

PHP 5 >= 5.2.0

int preg_last_error ()

Returns the error code of the last preg_ function call. The error codes correspond to the following constants:

  • PREG_NO_ERROR
  • PREG_INTERNAL_ERROR
  • PREG_BACKTRACK_LIMIT_ERROR
  • PREG_RECURSION_LIMIT_ERROR
  • PREG_BAD_UTF8_ERROR


[edit] preg_match_all

PHP >= 4

int preg_match_all ( string $pattern, string $subject, array &$matches [, int $flags [, int $offset]] )

Searches $subject for all matches to the regular expression given in $pattern and puts them in $matches in the order specified by $flags. After the first match is found, the subsequent searches are continued on from end of the last match.

The return value is the number of full pattern matches or FALSE if an error occurred.

Flags:

  • PREG_PATTERN_ORDER (default) orders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on.
  • PREG_SET_ORDER orders results so that $matches[0] is an array of first set of matches, $matches[1] is an array of second set of matches, and so on.
  • PREG_OFFSET_CAPTURE - If passed, for every occurring match the appendant string offset will also be returned. This changes the value of matches 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.

[edit] preg_match

PHP >= 4

int preg_match ( string $pattern, string $subject [, array &$matches [, int $flags [, int $offset]]] )

Searches $subject for a match to the regular expression given in $pattern. Searching stops after the first match is found.

The return value with either be 0 for no matches or 1 for a match found.

[edit] preg_quote

PHP >= 4

string preg_quote ( string $str [, string $delimiter] )

Take $str and puts a backslash in front of every character that is part of the regular expression syntax.

The special regular expression characters are: . \ + * ? [ ^ ] $ ( ) { } = ! < > | :

[edit] preg_replace_callback

PHP >= 4.0.5

mixed preg_replace_callback ( mixed $pattern, callback $callback, mixed $subject [, int $limit [, int &$count]] )

The behavior of this function is almost identical to preg_replace, except that instead of a replacement string parameter, a callback function is called. The callback function should return the replacement string.

[edit] preg_replace

PHP >= 4

mixed preg_replace ( mixed $pattern, mixed $replacement, mixed $subject [, int $limit [, int &$count]] )

Search $subject for matches to $pattern and replace them with $replacement.

$replacement may contain backreferences in the form of $n, where n is the number of the backreference.

Example:

$string = preg_replace('/(regular\sexpressions).*(rule)/i', '$1 sometimes $2', 'Regular expressions always rule.');
echo $string;

Output:

Regular expressions sometimes rule.

[edit] preg_split

PHP >= 4

array preg_split ( string $pattern, string $subject [, int $limit [, int $flags]] )

Split $subject by $pattern.

$flags is any combination of the following constants (combined with bitwise | operator):

  • PREG_SPLIT_NO_EMPTY - Only non-empty pieces will be returned.
  • PREG_SPLIT_DELIM_CAPTURE - Parenthesized expression in the delimiter pattern will be captured and returned as well
  • PREG_SPLIT_OFFSET_CAPTURE - For every occurring match the appendant string offset will also be returned. 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.