From 535a1f95e4414e294b7d4b11847c9e138585caee Mon Sep 17 00:00:00 2001 From: Torben Wilson Date: Fri, 18 Aug 2000 11:16:14 +0000 Subject: [PATCH] Closing Bug #5833: ereg_replace fails to use correct variable type. Noted that the $regs parameter to ereg() is limited to 10 elements. git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@30437 c90b9560-bf6c-de11-be94-00142212c4b1 --- functions/regex.xml | 68 +++++++++++++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 17 deletions(-) diff --git a/functions/regex.xml b/functions/regex.xml index 0ee18c0a9e..d83712b61c 100644 --- a/functions/regex.xml +++ b/functions/regex.xml @@ -106,13 +106,22 @@ $string = ereg_replace ("\n", "", $string); the substring starting at the second, and so on. $regs[0] will contain a copy of string. - + + If ereg finds any matches at all, $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. - + + + 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: @@ -127,11 +136,11 @@ if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) { - + See also eregi, ereg_replace, and eregi_replace. - + @@ -150,17 +159,17 @@ if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) { string string - + 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 @@ -170,11 +179,11 @@ if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) { 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: @@ -189,9 +198,34 @@ echo ereg_replace ("(( )is)", "\\2was", $string); + 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: + + <function>ereg_replace</function> Example + +<?php +/* This will not work as expected. */ +$num = 4; +$string = "This string has four words."; +$string = ereg_replace('four', $num, $string); +echo $string; /* Output: 'This string has words.' */ + +/* This will work. */ +$num = '4'; +$string = "This string has four words."; +$string = ereg_replace('four', $num, $string); +echo $string; /* Output: 'This string has 4 words.' */ +?> + + + + See also ereg, eregi, and eregi_replace. - +