php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #11448 str_replace
Submitted: 2001-06-12 19:07 UTC Modified: 2001-06-18 23:37 UTC
From: a2zofciv2 at hotmail dot com Assigned:
Status: Closed Package: Unknown/Other Function
PHP Version: 4.0.5 OS: cobalt linux
Private report: No CVE-ID: None
 [2001-06-12 19:07 UTC] a2zofciv2 at hotmail dot com
the following line:

$line = str_replace("pattern", Pattern(),$line);

where Pattern() is a function that returns a string.

Pattern executes everytime this code line is encountered, even if "pattern" is not a match on $line.

so if i loop through a file running that line, the function Pattern() will execute every single time, match or no match.

(my) common sense says this function should only execute when a match is found.  Is this a bug? I didn't know about it and something like this could seriously affect performance of a program.

Also I don't think the modules or any other of your requirements are relevant for this issue.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-06-13 10:57 UTC] sniper@php.net
Where does it say you can use function as the replacement?
You have to set it to variable first.

 [2001-06-18 23:37 UTC] rasmus@php.net
Uh, Jani, of course you can use a function there.
But the function will get evaluated to produce a string
which becomes the argument to str_replace().  That's just the way it works.  The function will be called every time.
If you don't want it called, add the appropriate logic to
not call it by checking for the match first with a strstr()
call.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 30 02:01:31 2024 UTC