php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #8597 problem in str_replace
Submitted: 2001-01-08 10:56 UTC Modified: 2001-01-08 11:01 UTC
From: jeff at procata dot com Assigned:
Status: Closed Package: *Function Specific
PHP Version: 4.0.2 OS: linux
Private report: No CVE-ID: None
 [2001-01-08 10:56 UTC] jeff at procata dot com
The following code:

$key = "test";
echo str_replace("{$key}", "Replaced", "abc {test} xyz");

produces incorrect output:

abc {Replaced} xyz

where the equivelant code:

echo str_replace("{test}", "Replaced", "abc {test} xyz");

produces the correct output:

abc Replaced xyz

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-01-08 11:01 UTC] cynic@php.net
this, though it may not be deemed as the most intuitive behavior, is the intended one. 

$a = array( 'aa' , 'bb' , 'cc' ) ;
echo str_replace("{$a[1]}", "Replaced", "abc {aa} xyz");

will produce "abc {Replaced} xyz", whereas 
echo str_replace("$a[1]", "Replaced", "abc {aa} xyz");
would produce a parse error. 

escape the braces.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 01 18:01:31 2024 UTC