From:             gerhard at tinned-mail dot net
Operating system: WinXP
PHP version:      5.1.1
PHP Bug Type:     PCRE related
Bug description:  preg_replace() distroys $GLOBALS

Description:
------------
PHP 5 PCRE Bug!?

We have in our program a function to log all $GLOBALS variables. This
function 
calls itself recursive to log all variables of a array and all its
sub-arrays.

This function is working fine in PHP 4.0.1 and up to 4.3.10 (the highest
of our 
test configuration.)

Since PHP5 (different versions) it was not working anymore when you call
the function with $GLOBALS as 
parameter. The logfile contained only one line ...
|-> GLOBALS = Array

After some hours i go it working by replacing the preg_replace() function
with
the ereg_replace() function.

The problem shows as follows:
After calling function preg_replace() the $GLOBALS array contails only 1
element
which is $GLOBALS. (i cheched it with count($GLOBALS);) after that the
check 
is_array() is always returning "1" instead of true or false.

after replacing the ...
preg_replace("/\r*\n/", " ", $entry); 
... with  ...
ereg_replace("\n", " ", $entry); 
ereg_replace("\r", " ", $entry); 
... the function is working without problems. PHP must be configured
correctly 
because this function preg_replace() is used very often in our program.
This 
function is also workinf fine when called with any other array. Only when

$GLOBALS is used to call this function it shows this behaviour.

We checked this behaviour with different PHP4.x (everything was working)
and some versions of PHP5 (on all the same problem - last tested version
5.1.1). And also on different OS (Linux, MacOSX, WinXP, Win2000).



Reproduce code:
---------------
<?php
/*****************************************************************************/
/* This file is part of Tinned-Mail.                                      
  */
/*****************************************************************************/

array_log($GLOBALS);

function array_log($array, $level = 0){
    // create level space
    $level_space = "";
    for($i=0; $i < $level; $i++){
        $level_space .= "    ";
    }
    
    // print array entries 
    foreach($array as $key => $value){
        $entry = $value;
        $entry = preg_replace("/\r*\n/", " ", $entry);
        $entry2 = "$level_space|-> $key = $entry\n";
        $FILE = fopen("array.log", "a");
        fwrite($FILE, $entry2);
        fclose($FILE);
        echo is_array($value);
        if(is_array($value)){
            if($key == "GLOBALS"){ continue; }
            array_log($value, $level + 1);
        }
    }
}

?>

Expected result:
----------------
Logfile with PHP 5.1.1:
|-> GLOBALS = Array

Logfile with 4.x.x (as it should look like):
|-> HTTP_ENV_VARS = Array
    |-> DBROOT = /dev/null
    |-> BOOT_FILE = /boot/vmlinuz
    |-> HOSTNAME = mail
    |-> CONSOLE = /dev/console
    |-> LD_LIBRARY_PATH = :/lib
    .
    .
    .
|-> HTTP_POST_FILES = Array
|-> GLOBALS = Array
|-> GATEWAY_INTERFACE = CGI/1.1
|-> SERVER_PROTOCOL = HTTP/1.0
|-> REQUEST_METHOD = GET
|-> QUERY_STRING = 
.
.
.



Actual result:
--------------
Replace ...
$entry = preg_replace("/\r*\n/", " ", $entry);
... With ...
$entry = ereg_replace("\r", "", $entry);
$entry = ereg_replace("\n", " ", $entry);

After changing it to ereg_replace() it is working with PHP4.0.1, 4.3.10
and 5.1.1



-- 
Edit bug report at http://bugs.php.net/?id=35733&edit=1
-- 
Try a CVS snapshot (PHP 4.4): 
http://bugs.php.net/fix.php?id=35733&r=trysnapshot44
Try a CVS snapshot (PHP 5.1): 
http://bugs.php.net/fix.php?id=35733&r=trysnapshot51
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=35733&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=35733&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=35733&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=35733&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=35733&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=35733&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=35733&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=35733&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=35733&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=35733&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=35733&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=35733&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=35733&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=35733&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=35733&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=35733&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=35733&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=35733&r=mysqlcfg

Reply via email to