Bug #76571 [Opn->Nab]: str_replace fails to replace string in special output buffered environment
| From: | requinix@php.net | Date: | Tue, 03 Jul 2018 08:41:27 +0000 |
| Subject: | Bug #76571 [Opn->Nab]: str_replace fails to replace string in special output buffered environment | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-216097@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=76571&edit=1
ID: 76571
Updated by: requinix@php.net
Reported by: roland at nextendweb dot com
Summary: str_replace fails to replace string in special
output buffered environment
-Status: Open
+Status: Not a bug
Type: Bug
Package: Output Control
PHP Version: 7.2.7
Block user comment: N
Private report: N
New Comment:
http://php.net/manual/en/function.ob-start.php
> The [output_callback] function will be called when the output buffer is flushed (sent) or
> cleaned (with
> ob_flush(), ob_clean() or similar function) or when the output buffer is flushed to the browser
> at the end of the
> request.
1. When you echo "Hello there", the callback is *not invoked immediately*
2. ob_get_contents returns "Hello there" back
3. ob_clean sends "Hello there" to the callback, which replaces using "World",
but the result is discarded
4. You echo the original "Hello there" again
5. ob_end_flush sends the new "Hello there" to the callback, which replaces using
"What?", and the result is sent
Is there a reasonable use case behind this problem? Because otherwise what you should be doing is
simply
ob_start('a::parse_output');
echo "Hello there";
ob_end_flush();
Previous Comments:
------------------------------------------------------------------------
[2018-07-03 07:22:46] roland at nextendweb dot com
Description:
------------
http://sandbox.onlinephpfunctions.com/code/08eee50202cb2e8ce5b6e849aacb7c5bf37de4ba
Test script:
---------------
<?php
class a {
private static $bug = 'World';
public static function parse_output($buffer) {
$buffer = str_replace('there', self::$bug, $buffer);
self::$bug = 'What?';
return $buffer;
}
}
ob_start('a::parse_output');
echo "Hello there";
$content = ob_get_contents();
ob_clean();
echo $content;
ob_end_flush();
Expected result:
----------------
Hello World
Actual result:
--------------
Hello What?
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=76571&edit=1