php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #1362 unusual str_replace failure when no whitespace follows
Submitted: 1999-04-29 16:03 UTC Modified: 1999-05-11 07:49 UTC
From: alex at ibrado dot com Assigned:
Status: Closed Package: Misbehaving function
PHP Version: 3.0.7 OS: Red Hat Linux 5.2 kernel 2.2.6
Private report: No CVE-ID: None
 [1999-04-29 16:03 UTC] alex at ibrado dot com
If the following script seems convoluted/unneccessarily complex, it's because the do_it function is a shortened, combined version of several actual functions. This is adapted from a dynamic block template class I'm working on (similar to FastTemplate).

<?

function do_it($str,$var,$val) {
	// $str => string to process
	// $var => variable to replace
	// $val => value of the variable

	ereg("<!-- Start -->(.+)<!-- End -->",$str,$reg);

	// Get the template block

	$block=$reg[0]; 	print "\nBLOCK: [$block]\n";

	// Get the stuff between Begin and End

	$innards=$reg[1]; 	print "\nINNARDS: [$innards]\n";

	// Replace the variable with its value

	$updated_innards=str_replace("{$var}",$val,$innards);

	// Using ereg_replace instead of str_replace always works

	$updated_innards_ereg_replace=ereg_replace("{$var}",$val,$innards);

	print "\n*********************************\n";
	print "*\n* UPDATED_INNARDS: [$updated_innards]\n";
	print "*\n* UPDATED_INNARDS_EREG_REPLACE: [$updated_innards_ereg_replace]\n";
	print "*\n*********************************\n";

	// Replace the template block with the text containing 
	//   the evaluated variables

	$updated_text=str_replace($block,$updated_innards,$str);

	print "\nUPDATED_TEXT: [$updated_text]\n";

}


Header("Content-type: text/plain");
 
$var="addr";			// {variable} to replace
$val="you@address.com";	// value to replace {variable} with

// The only difference between $text1 and $text2 is a space 
//	after the second {addr} in $text1

$text1="<UL><!-- Start --><LI>{addr}, that's {addr} <!-- End --></UL>";
$text2="<UL><!-- Start --><LI>{addr}, that's {addr}<!-- End --></UL>";


// Show $text1 with the <!-- Start --> .. <!-- End --> blocks
//	replaced with "<LI>you@youraddress, that's you@youraddress"

print "\n=============================== USING TEXT1 (NO PROBLEM) ======\n";
print do_it($text1,$var,$val);


// Try to do the same for $text2

print "\n\n\n=============================== USING TEXT2 ======\n";
print do_it($text2,$var,$val);

?>

-- Configured with all optional modules
-- php3.ini not changed
-- simple substitution using str_replace works:

<? 

$text1="<!-- Begin --><LI>{addr} <!-- End -->"; // with the space
$text2="<!-- Begin --><LI>{addr}<!-- End -->";

$text1=str_replace("{addr}","you@address.com",$text1);
$text2=str_replace("{addr}","you@address.com",$text2);

Header("Content-type: text/plain");
print "TEXT1: [$text1]\n";
print "TEXT2: [$text2]\n";

?>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [1999-04-30 20:13 UTC] alex at ibrado dot com
Not ALL optional modules, what was I thinking?! ;) Here's the configure line:

./configure --with-imap  --with-gd --with-apxs --with-imagick --with-mysql --with-zlib --with-dbase 
 [1999-05-02 12:27 UTC] sas at cvs dot php dot net
There were a number of problems in str_replace which we believe to have fixed. There were no known issues in regard to white spaces, but you might try the CVS version or apply the following patch to your PHP 3.0.7 sources:

http://cvs.php.net/cvsweb.cgi/functions/string.c.diff?r1=1.178&r2=1.179
 [1999-05-11 07:49 UTC] sas at cvs dot php dot net
Fixed in CVS.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Mon Jul 06 13:00:02 2026 UTC