Regular Expression Functions (POSIX Extended)Regexps
PHP also supports regular expressions using a Perl-compatible syntax
using the PCRE functions. Those functions
support non-greedy matching, assertions, conditional subpatterns, and a
number of other features not supported by the POSIX-extended regular
expression syntax.
These regular expression functions are not binary-safe. The PCRE functions are.
Regular expressions are used for complex string manipulation in
PHP. The functions that support regular expressions are:
eregereg_replaceeregieregi_replacesplitspliti
These functions all take a regular expression string as their
first argument. PHP uses the POSIX extended regular expressions
as defined by POSIX 1003.2. For a full description of POSIX
regular expressions see the regex man pages included in the regex
directory in the PHP distribution. It's in manpage format, so
you'll want to do something along the lines of man
/usr/local/src/regex/regex.7 in order to read it.
Regular Expression Examples
", $string);
/* Put a tag at the beginning of $string. */
$string = ereg_replace ("$", " ", $string);
/* Put a ; tag at the end of $string. */
$string = ereg_replace ("\n", "", $string);
/* Get rid of any newline
characters in $string. */
]]>
eregRegular expression matchDescriptionint eregstring patternstring stringarray
regspreg_match, which uses a Perl-compatible
regular expression syntax, is often a faster alternative to
ereg.
Searches a string for matches to the regular
expression given in pattern.
If matches are found for parenthesized substrings of
pattern and the function is called with
the third argument regs, the matches will
be stored in the elements of the array
regs. $regs[1] will contain the substring
which starts at the first left parenthesis; $regs[2] will contain
the substring starting at the second, and so on. $regs[0] will
contain a copy of the complete string matched.
Up to (and including) PHP 4.1.0 $regs will be
filled with exactly ten elements, even though more or fewer than
ten parenthesized substrings may actually have matched. This has
no effect on ereg's ability to match more
substrings. If no matches are found, $regs
will not be altered by ereg.
Searching is case sensitive.
Returns &true; if a match for pattern was
found in string, or &false; if no matches
were found or an error occurred.
The following code snippet takes a date in ISO format
(YYYY-MM-DD) and prints it in DD.MM.YYYY format:
ereg Example
See also eregi,
ereg_replace,
eregi_replace and
preg_match.
ereg_replaceReplace regular expressionDescriptionstring ereg_replacestring patternstring replacementstring stringpreg_replace, which uses a Perl-compatible
regular expression syntax, is often a faster alternative to
ereg_replace.
This function scans string for matches to
pattern, then replaces the matched text
with replacement.
The modified string is returned. (Which may mean that the
original string is returned if there are no matches to be
replaced.)
If pattern contains parenthesized
substrings, replacement may contain
substrings of the form
\\digit, which will
be replaced by the text matching the digit'th parenthesized
substring; \\0 will produce the entire
contents of string. Up to nine substrings may be used.
Parentheses may be nested, in which case they are counted by the
opening parenthesis.
If no matches are found in string, then
string will be returned unchanged.
For example, the following code snippet prints "This was a test"
three times:
ereg_replace Example
One thing to take note of is that if you use an integer value as
the replacement parameter, you may not get
the results you expect. This is because
ereg_replace will interpret the number as
the ordinal value of a character, and apply that. For instance:
ereg_replace Example
]]>
Replace URLs with links
[:space:]]+[[:alnum:]/]",
"\\0", $text);
]]>
See also ereg, eregi,
eregi_replace, and preg_match.
eregicase insensitive regular expression matchDescriptionint eregistring patternstring stringarray
regs
This function is identical to ereg except
that this ignores case distinction when matching alphabetic
characters.
eregi example
See also ereg,
ereg_replace, and
eregi_replace.
eregi_replacereplace regular expression case insensitiveDescriptionstring eregi_replacestring patternstring replacementstring string
This function is identical to ereg_replace
except that this ignores case distinction when matching
alphabetic characters.
See also ereg, eregi,
and ereg_replace.
splitsplit string into array by regular expressionDescriptionarray splitstring patternstring stringint
limitpreg_split, which uses a Perl-compatible
regular expression syntax, is often a faster alternative to
split.
Returns an array of strings, each of which is a substring of
string formed by splitting it on
boundaries formed by the regular expression
pattern. If limit
is set, the returned array will contain a maximum of
limit elements with the last element
containing the whole rest of string. If
an error occurs, split returns &false;.
To split off the first four fields from a line from
/etc/passwd:
split Example
If there are n occurences of
pattern, the returned array will contain
n+1 items. For example, if
there is no occurence of pattern, an array with
only one element will be returned. Of course, this is also true if
string is emply.
To parse a date which may be delimited with slashes, dots, or
hyphens:
split Example
\n";
]]>
Note that pattern is case-sensitive.
Note that if you don't require the power of regular expressions,
it is faster to use explode, which doesn't
incur the overhead of the regular expression engine.
For users looking for a way to emulate Perl's $chars =
split('', $str) behaviour, please see the examples for
preg_split.
Please note that pattern is a regular
expression. If you want to split on any of the characters which
are considered special by regular expressions, you'll need to
escape them first. If you think split (or
any other regex function, for that matter) is doing something
weird, please read the file regex.7,
included in the regex/ subdirectory of the
PHP distribution. It's in manpage format, so you'll want to do
something along the lines of man
/usr/local/src/regex/regex.7 in order to read it.
See also:
preg_split,
spliti,
explode,
implode,
chunk_split and
wordwrap.
spliti
Split string into array by regular expression case insensitive
Descriptionarray splitistring patternstring stringint
limit
This function is identical to split except
that this ignores case distinction when matching alphabetic
characters.
See also
preg_spliti,
split,
explode,
and implode.
sql_regcase
Make regular expression for case insensitive match
Descriptionstring sql_regcasestring string
Returns a valid regular expression which will match
string, ignoring case. This expression is
string with each character converted to a
bracket expression; this bracket expression contains that
character's uppercase and lowercase form if applicable, otherwise
it contains the original character twice.
sql_regcase Example
prints [Ff][Oo][Oo] [Bb][Aa][Rr].
This can be used to achieve case insensitive pattern matching in
products which support only case sensitive regular expressions.