php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30341 globals not available in functions called from preg_replace_callback
Submitted: 2004-10-06 19:36 UTC Modified: 2004-10-07 09:07 UTC
From: f_nietsje at hotmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.0.1 OS: Win NT
Private report: No CVE-ID: None
 [2004-10-06 19:36 UTC] f_nietsje at hotmail dot com
Description:
------------
I use preg_replace_callback to substitute parts of an expression with either true or false after which the expression can be evaluated using eval:

_q1,1,1-2 & _q2,3-4,7
should become
true & false
which can be evaluated to false.

The strings represent answers to questions (it's for an interview system)

The problem is that the answers are stored in a global array. The (global) array can not be read from a function that is called from preg_replace_callback

It would be nice if somebody can fix this :-)

Reproduce code:
---------------
function parseqstring($qstring)
{    
    global $qre;
    preg_match_all('/(\d+-\d+|\d+)/', $qstring, $numbers);
    $q = array_shift($numbers[0]);
    foreach($numbers[0] as $number)
    {
        //if its a number check the answer
        if(is_numeric($number))
        {
            //if the answer is there return true because it's all 'or' conditions
            if($qre[$q]['answers'][$number]==1)
            {
                return 'true';
            }
        }
        //if its a range check all the numbers in the range
        if(preg_match('/(\d+)-(\d+)/', $number, $match))
        {
            for($i=$match[1];$i<=$match[2];$i++)
            {
                if($qre[$q]['answers'][$i])
                {
                    return 'true';
                }
            }
        }
    }
    return 'false';
}




Expected result:
----------------
input: 
$qstring = _q1,1,3-4
$qre[1]['answers'][3]
output:
'true'

Actual result:
--------------
'false'

Patches

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-07 09:07 UTC] derick@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions. 

Thank you for your interest in PHP.

derick@kossu:~$ cat bug30341.php
<?php
        $test = "works fine\n";

        function cb() {
                global $test;

                echo $test;
                return 42;
        }

        echo preg_replace_callback("/foo/", "cb", "foofoo");
?>
derick@kossu:~$ php-5.0dev bug30341.php

works fine
works fine
4242

Works fine as you can see, there must be an error in your code.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Aug 15 18:01:29 2024 UTC