How to use regular expressions in PHP
Keywords: PHP
In PHP, expressions are used to handle complex strings. The following Regex expressions are supported:
Ereg ()
Ereg_replace ()
Eregi ()
Eregi_replace ()
Split ()
(1) Ereg,eregi
This is the regular expression matching function, the former is case-sensitive, the latter is irrelevant.
Usage:
Ereg (regular expression, string, [match partial array name]);
Regular expressions in PHP3.0 are broadly similar to those used in grep.
(2) Ereg_replace,eregi_replace
These are replacement functions.
Usage:
Ereg_replace (Regular expression, substitution string, original string);
There is a strtr in the string handler function, which is a "translation" function, similar to the tr/.../.../in Perl,
Usage:
STRTR (String, "from", "to");
For example:
STRTR ("Aaabb", "AB", "CD") returns "CCCDD".
(3) Split
Similar to the EXPLODE function, but this time it is possible to split a string where it matches a regular expression.
Usage:
Split (regular expression, string, [how many items before removal]);
These functions use a regular string as the first argument. PHP uses the extended regular string defined by the POSIX 1003.2 standard.
To see the full description of POSIX regular expressions see the man page under the Regex directory in the PHP package.
Regular Expression Examples:
Ereg ("abc", $string);
/* Returns true if "abc" is found anywhere in $string. */
Ereg ("^abc", $string);
/* Returns true if "abc" is found at the beginning of $string. */
Ereg ("abc$", $string);
/* Returns true if "abc" is found at the end of $string. */
Eregi ("(ozilla.[ 23]| MSIE.3) ", $HTTP _user_agent);
/* Returns True if client browser is Netscape 2, 3 or MSIE 3. */
Ereg ("([: alnum:]]+)" ([: alnum:]]+), $string, $regs);
/* Places Three space separated words into $regs [1], $regs [2] and $regs [3]. */
Ereg_replace ("^", "", $string);
/* Put a tag at the beginning of $string. */
Ereg_replace ("$", "", $string);
/* Put a tag at the end of $string. */
Ereg_replace ("", "", $string);
/* Get rid of any carriage return characters in $string. */
http://www.bkjia.com/PHPjc/532263.html www.bkjia.com true http://www.bkjia.com/PHPjc/532263.html techarticle How to use regular expressions in PHP Keywords: php expression in PHP is used for complex string processing. The following Regex expressions are supported: Ereg () ereg_replace () eregi () ere ...