Bug #18440 Updated: str_replace array replace can't handle strings in quotes

From: Date: Fri, 19 Jul 2002 23:14:15 +0000
Subject: Bug #18440 Updated: str_replace array replace can't handle strings in quotes
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-14666@lists.php.net to get a copy of this message
ID: 18440 Updated by: rasmus@php.net Reported By: yml@dtlink.com -Status: Feedback +Status: Bogus Bug Type: Strings related Operating System: Redhat 6.1 PHP Version: 4.2.1 New Comment: This script: $source = 'TEXT'; $replace = '<tag attribute="1"><tag attribute="blah">'; $template = '<attribute="TEXT">'; $expandedStr = str_replace( $source, $replace, $template ); print( "<h1>Result is: '$expandedStr'</h1>" ); Gives me: Result is: '<attribute="<tag attribute="1"><tag attribute="blah">">' I don't see a bug here. Try running it from the command line. Perhaps your browser is eating something. Previous Comments: ------------------------------------------------------------------------ [2002-07-19 19:14:10] sniper@php.net I can not reproduce this with latest CVS (php 4.3.0-dev) or with PHP 4.2.1. Are you sure you're using 4.2.1? ------------------------------------------------------------------------ [2002-07-19 17:48:30] yml@dtlink.com Some more observations: $source = 'TEXT'; $replace = '<tag attribute="1"><tag attribute="blah">'; $template = '<attribute="TEXT">'; $expandedStr = str_replace( $source, $replace, $template ); print( "<h1>Result is: '$expandedStr'</h1>" ); Yields: <h1>Result is: '<attribute="<tag attribute="1><tag attribute="blah">">'</h1> as you can see, the closing " from attribute="1 is missing. HOWEVER, if we remove the angle brackets from the template (so that $template= 'attribute="TEXT"') the result is: <h1>Result is: 'attribute="<tag attribute="1"><tag attribute="blah">"'</h1> i.e. the double quote is correctly maintained. ------------------------------------------------------------------------ [2002-07-19 17:35:22] yml@dtlink.com Stock install of php 4.2.1 with the default php.ini file. The following script demonstrates that str_replace() munches double quotes when it shouldn't. <? $source_vars[1] = '${TEXT}'; $replace_vars[1] = '<font size="1"><a href="javascript:self.close();">Close Window</a></font>'; $source_string = '<a href="${TEXT}" >'; $expandedStr = str_replace( @$source_vars, @$replace_vars, @$source_string ); print( "<h1>EXPANDED_STR is: '$expandedStr'" ); ?> You will notice that the output of this script is: <h1>EXPANDED_STR is: '<a href="<font size="1><a href="javascript:self.close();">Close Window</a></font>" >' Note the missing " at on the font tag size="1 ... If you remove the double quotes from source_string as in: $source_string = '<a href=${TEXT} >'; Then the output is correct as: <h1>EXPANDED_STR is: '<a href=<font size="1"><a href="javascript:self.close();">Close Window</a></font> >' well, aside from the fact that the href= needs the double quotes. Originally I thought this was restricted to array reference replacements but if you use the straight string form of str_replace the same behavior happen ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=18440&edit=1

« previous php.bugs (#14666) next »