Bug #14879 Updated: ereg_replace incorrectly functioning

From: Date: Sun, 06 Jan 2002 00:05:45 +0000
Subject: Bug #14879 Updated: ereg_replace incorrectly functioning
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-75997@lists.php.net to get a copy of this message
ID: 14879 Updated by: jimw Reported By: jhise@linuxforbusiness.org Status: Bogus Bug Type: Scripting Engine problem Operating System: Linux-Mandrake 8.1 PHP Version: 4.0.6 New Comment: ereg_replace is behaving as designed, more or less. the behavior of an empty first parameter is undefined (although with the bundled regular expression engine, it will leave the string unchanged and display an error message). i'd argue the behavior you're seeing is correct. it is replacing the empty spaces in the strings with the replacement string. (preg_replace() behaves this way.) you are seeing the html tags unescaped because that is what some browsers do with invalid-looking html tags (like you'll get with a pipe between every character). php doesn't do any internal escaping of html characters. Previous Comments: ------------------------------------------------------------------------ [2002-01-05 19:01:19] mfischer@php.net No idea what your points are. The first one gives a warning REG_EMPTY and the second does what it is expected to do: replace all space characters with pipe symbols (tested with 4.0.6 and 4.1.1). And, really, separate code from description. ------------------------------------------------------------------------ [2002-01-05 18:35:56] jhise@linuxforbusiness.org <? ## Assign a bit of HTML to two source variables # $space_source_text = $null_source_text = "<b>PHP</b> <i>is</i> a useful <u>tool</u>"; ## Replace a substring within the the source variables # with another substring $null_source_text = ereg_replace("", "|" $null_source_text); $space_source_text = ereg_replace(" ", "|", $space_source_text); ## Print the new values of the source variables # print("This is the result of calling ereg_replace with an empty parameter.<hr>$null_source_text<br><br><br>"); print("This is the result of calling ereg_replace with a space as the value of the parameter.<hr>$space_source_text"); ## Conclusion # # To me it seems that there are two significant points to make about the behaviour of ereg_replace as demonstrated # in this example. # # The first point is that, when passing an empty value in the first parameter, the ereg_replace function inserts # the second parameter between every character in the string, therefor increasing the overall size of the string. # It is inserting -- not replacing as the function name implies # # The second point is that, when passing an empty value in the first parameter, the ereg_replace function does # not evaluate the HTML tags so that they are correctly rendered to the browser. # # Please keep in mind that I have yet to see the definition of ereg_replace. # # At first, I thought the ereg_replace function was possibly checking to see if the first parameter was empty, and if # so, return execution immediatly to the caller. However, this doesn't appear to be the case because it is obviously # transversing the character array because it is inserting the second parameter between each character. # # I'm guessing that PHP does some internal escaping of HTML characters and the empty first parameter is somehow # goofing up the translation back to clean HTML # ## ?> ------------------------------------------------------------------------ [2002-01-05 18:33:02] jhise@linuxforbusiness.org <? ## Assign a bit of HTML to two source variables # $space_source_text = $null_source_text = "<b>PHP</b> <i>is</i> a useful <u>tool</u>"; ## Replace a substring within the the source variables # with another substring $null_source_text = ereg_replace("", "|", $null_source_text); $space_source_text = ereg_replace(" ", "|", $space_source_text); ## Print the new values of the source variables # print("This is the result of calling ereg_replace with an empty parameter.<hr>$null_source_text<br><br><br>"); print("This is the result of calling ereg_replace with a space as the value of the parameter.<hr>$space_source_text"); ## Conclusion # # To me it seems that there are two significant points to make about the behaviour of ereg_replace as demonstrated # in this example. # # The first point is that, when passing an empty value in the first parameter, the ereg_replace function inserts # the second parameter between every character in the string, therefor increasing the overall size of the string. # It is inserting -- not replacing as the function name implies # # The second point is that, when passing an empty value in the first parameter, the ereg_replace function does # not evaluate the HTML tags so that they are correctly rendered to the browser. # # Please keep in mind that I have yet to see the definition of ereg_replace. # # At first, I thought the ereg_replace function was possibly checking to see if the first parameter was empty, and if # so, return execution immediatly to the caller. However, this doesn't appear to be the case because it is obviously # transversing the character array because it is inserting the second parameter between each character. # # I'm guessing that PHP does some internal escaping of HTML characters and the empty first parameter is somehow # goofing up the translation back to clean HTML # ## ?> ------------------------------------------------------------------------ Edit this bug report at http://bugs.php.net/?id=14879&edit=1

« previous php.dev (#75997) next »