php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #18440 str_replace array replace can't handle strings in quotes
Submitted: 2002-07-19 17:35 UTC Modified: 2002-07-19 19:14 UTC
From: yml at dtlink dot com Assigned:
Status: Not a bug Package: Strings related
PHP Version: 4.2.1 OS: Redhat 6.1
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
25 + 6 = ?
Subscribe to this entry?

 
 [2002-07-19 17:35 UTC] yml at dtlink dot 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-07-19 17:48 UTC] yml at dtlink dot 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 19:14 UTC] 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 19:14 UTC] rasmus@php.net
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.
 [2002-07-19 19:27 UTC] yml at dtlink dot com
Definitely reproducible under PHP 4.2.1. Here's the output from phpinfo on the box where the problems is presenting:

PHP Version 4.2.1

System Linux forge.yml.com 2.2.12-20 #1 Mon Sep 27 10:40:35 EDT 1999 i686 unknown
Build Date Jul 10 2002 19:17:13
Configure Command './configure' '--with-mysql=/usr/local/mysql' '--with-apxs=/usr/local/apache/bin/apxs' '--with-gd'
Server API Apache
Virtual Directory Support disabled
Configuration File (php.ini) Path /usr/local/lib/php.ini
Debug Build no
Thread Safety disabled

Zend logo This program makes use of the Zend Scripting Language Engine:
Zend Engine v1.2.0, Copyright (c) 1998-2002 Zend Technologies
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 08:01:28 2024 UTC