php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #21287 str_replace behaves strangely, not replacing valid key/val pairs
Submitted: 2002-12-30 06:00 UTC Modified: 2002-12-30 15:35 UTC
From: tit dot petric at telemach dot net Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 4.3 OS: linux debian
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: tit dot petric at telemach dot net
New email:
PHP Version: OS:

 

 [2002-12-30 06:00 UTC] tit dot petric at telemach dot net
i have a template engine running, in it a string (content hereon) with keys and an array (parsevals hereon) with key/values that need to be replaced in the content

this is what i do:

foreach ($this->_parsevals as $key=>$val) {
        $retval = str_replace('{'.$key.'}',$val,$retval);
}

after that i do a preg_match_all to find all the remaining keys in the content with this regexp: '/\{[A-Za-z][-_A-Za-z0-9]*\}/', and find existing keys, which also exist in $this->_parsevals..


if (preg_match_all($this->regex_var,$retval,$result)) {
        foreach ($result[0] as $val) {
                $key = substr($val,1,-1);
                if (isset($this->_parsevals[$key])) {
                        condor_assert($key." exists in parsevals?!?!?");
                        $retval = str_replace($val, $this->_parsevals[$key],$retval);
                }
        }
}

condor_assert is a basic log function, and its activated with that error message in there... another str_replace seems to solve the problem of the "undefined variable" case..

i tried to reproduce this in a short-self-contained script but failed, i will test it on 4.3 asap

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-12-30 10:56 UTC] tit dot petric at telemach dot net
behaviour confirmed also on 4.3

http://sunshine.krneki.org/phpinfo.php

i've tried the script with and without the phpA zend_extension, and if it was active then i tried it in disabled mode (php_value phpa off - inside .htaccess)

currentlly the php.ini file is the same as php.ini-recommended inside the 4.3.0 distribution

i now definetly check if i str_replace that key inside the first loop and the logging is as follows (same as before):

(1.228) 0.0028 replace block_spacing_height => <img src="modules/themes/base/images/pixel.gif" align="middle" border="0" alt="" height="10">
(1.232) 0.0040 block_spacing_height exists in parsevals?!?!?
(1.233) 0.0017 Undefined variables exist in root (site_footer.tpl):
Array
(
    [0] => Array
        (
            [0] => {block_spacing_height}
        )

)

ofcourse after the repeated str_replace the key is accurately replaced so i dont know where the problem could live (if its on my side)...

is there any way i can track this more accuratelly than with my own functions? debug_backtrace() perhaps?
 [2002-12-30 15:35 UTC] black@php.net
it appears i had a nested template to replace a key, after the key/value pairs were substituted already - thus resulting in that little error

totally my fault, seems that most of my bugs are :P
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 17 14:04:04 2025 UTC