php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #14879 ereg_replace incorrectly functioning
Submitted: 2002-01-05 18:33 UTC Modified: 2002-01-05 19:05 UTC
From: jhise at linuxforbusiness dot org Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.0.6 OS: Linux-Mandrake 8.1
Private report: No CVE-ID: None
 [2002-01-05 18:33 UTC] jhise at linuxforbusiness dot 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
	#
	##
?>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-01-05 18:35 UTC] jhise at linuxforbusiness dot 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 19:01 UTC] 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 19:05 UTC] jimw@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Oct 06 07:01:27 2024 UTC