03.0 PHP Regular Expression
03.0 PHP Regular Expression
1
Regular Expressions in PHP
For instance, in the earlier phone number example, the users defined rules
that dictated the format of the phone number entered, but not the digits
contained in the phone number
POSIX and PCRE - share a syntax (although they differ in their support for
some features and character classes)
Each regex is made up of one or more:
Characters
Special characters (sometimes called metacharacters)
Character classes
Groups of characters.
To look for the literal meaning of special characters (line anchors,
qualifiers), their special meaning should be escaped, using \ (backslash)
To look for a literal period or dot, use the escape character \: \..
Line anchors are special metacharacters that match the beginning or end
of a line, but don't capture any text (see Table 1).
For example, if a line begins with the letter a, a line anchor in the expression
^a doesn't capture the letter a, but does match the beginning of the line
POSIX PCREs
int ereg ( string $pattern , string $string ) int preg_match ( string $pattern , string $string)
- Searches a string for matches to the regular
expression given in pattern in a case-sensitive
way. - Searches string for a match to the
- Returns the length of the matched string if a regular expression given in pattern.
match for pattern was found in string , or FALSE - Returns 1 if the matches given , 0 if
(0) if no matches were found or an error it does not, or FALSE if an error
occurred.
occurred.
int eregi (string $pattern , string $string )
The i modifier, makes the regex
case-insensitive.
- Is similar to ereg(), except that it's case-
insensitive. No need to call a different method
The regex [-[:digit:]]{12} looks for 12 characters that are digits or hyphens.
It's a little sloppy for use with a phone number, and it can instead be written like
this: ^[0-9]{3}-[0-9]{3}-[0-9]{4}$.
The alternative expression is much more exact. It looks for the beginning of the
line (^), followed by a group of three digits ([0-9]{3}), a hyphen (-), another
group of three digits, another hyphen, a group of four digits, and then the end
of the line ($).
(In regex, [0-9] and [:digit:] are essentially the same thing; you may prefer [0-9]
because it's shorter.)
Master Shkencor Sisteme Informacioni ne Ekonomi 1/25/2021
12 POSIX eregi() example
eregi() example
When you execute this example, it prints Found match! because hello is
found as long as you do the search with case ignored. If you were using
ereg, the match would fail.
This example uses the regex ^[a-z]+$ to search for any letter a through z found
one or more times ([a-z]+) between the beginning (^) and the end ($) of the
line
PCRE functions require the expression to begin and end with a delimiter. In most
examples, the delimiter is a single / found at the beginning and end of the
expression, inside the quotes. It's important to remember that this delimiter isn't
part of the expression.
After the last delimiter in a PCRE, you can add a modifier that changes the
behavior of the regex. The i modifier, for example, makes the regex case-
insensitive.
POSIX PCREs
string ereg_replace ( string $pattern , mixed preg_replace ( mixed $pattern,
string $replacement, string $string ) mixed $replacement, string $string )
-Scans string for matches to pattern,
-Scans string for matches to pattern, then then replaces the matched text
replaces the matched text with replacement. with replacement.
- The modified string is returned. If no matches
are found in , then it will be returned unchanged - Returns an array if the parameter is
an array, or a string otherwise. If
matches are found, the new string
string eregi_replace ( string $pattern , will be returned, otherwise string will
string $replacement, string $string ) be returned unchanged or NULL if an
error occurred.
- Is similar to ereg_replace(), except that it's The i modifier, makes the regex
case-insensitive. case-insensitive.
No need to call a different method
Master Shkencor Sisteme Informacioni ne Ekonomi 1/25/2021
16 POSIX ereg_replace() example
ereg_replace() example