#30341 [Opn->Bgs]: globals not available in functions called from preg_replace_callback
| From: | derick@php.net | Date: | Thu, 07 Oct 2004 07:07:07 +0000 |
| Subject: | #30341 [Opn->Bgs]: globals not available in functions called from preg_replace_callback | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-67229@lists.php.net to get a copy of this message | ||
ID: 30341
Updated by: derick@php.net
Reported By: f_nietsje at hotmail dot com
-Status: Open
+Status: Bogus
Bug Type: Scripting Engine problem
Operating System: Win NT
PHP Version: 5.0.1
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2004-10-06 19:36:53] 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'
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=30341&edit=1