From 66e0bc53fb099428a35d3151b0033fc57c506b63 Mon Sep 17 00:00:00 2001 From: jim winstead Date: Fri, 2 Nov 2001 02:18:24 +0000 Subject: [PATCH] steer people towards pcre functions. incorporate notes and examples from user notes. git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@61382 c90b9560-bf6c-de11-be94-00142212c4b1 --- functions/regex.xml | 77 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 13 deletions(-) diff --git a/functions/regex.xml b/functions/regex.xml index 2c84eb76bf..6af2b5178c 100644 --- a/functions/regex.xml +++ b/functions/regex.xml @@ -1,10 +1,25 @@ - + 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: @@ -35,9 +50,6 @@ 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. - - - @@ -63,11 +75,11 @@ ereg ("([[:alnum:]]+) ([[:alnum:]]+) ([[:alnum:]]+)", $string,$regs); /* Places three space separated words into $regs[1], $regs[2] and $regs[3]. */ -$string = ereg_replace ("^", "<BR>", $string); -/* Put a <BR> tag at the beginning of $string. */ +$string = ereg_replace ("^", "<br />", $string); +/* Put a <br /> tag at the beginning of $string. */ -$string = ereg_replace ("$", "<BR>", $string); -/* Put a <BR> tag at the end of $string. */ +$string = ereg_replace ("$", "<br />", $string); +/* Put a <br /> tag at the end of $string. */ $string = ereg_replace ("\n", "", $string); /* Get rid of any newline @@ -94,6 +106,13 @@ $string = ereg_replace ("\n", "", $string); + + + preg_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. @@ -140,8 +159,9 @@ if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) { See also eregi, - ereg_replace, and - eregi_replace. + ereg_replace, + eregi_replace and + preg_match. @@ -161,6 +181,13 @@ if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) { string string + + + preg_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 @@ -224,9 +251,18 @@ echo $string; /* Output: 'This string has 4 words.' */ + + + Replace URLs with links + +$text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", + "<a href=\"\\0\">\\0</a>", $text); + + + See also ereg, eregi, - and eregi_replace. + eregi_replace, and preg_match. @@ -252,6 +288,14 @@ echo $string; /* Output: 'This string has 4 words.' */ This function is identical to ereg except that this ignores case distinction when matching alphabetic characters. + + <function>eregi</function> example + +if (eregi("z", $string)) { + echo "'$string' contains a 'z' or 'Z'!"; +} + + See also ereg, @@ -305,6 +349,13 @@ echo $string; /* Output: 'This string has 4 words.' */ + + + preg_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 @@ -321,7 +372,7 @@ echo $string; /* Output: 'This string has 4 words.' */ <function>split</function> Example -$passwd_list = split (":", $passwd_line, 5); +list($user,$pass,$uid,$gid,$extra)= split (":", $passwd_line, 5); @@ -359,7 +410,7 @@ echo "Month: $month; Day: $day; Year: $year<br>\n"; - For users looking for a way to emulate perl's $chars = + For users looking for a way to emulate Perl's $chars = split('', $str) behaviour, please see the examples for preg_split.