From:             richard dot piatkowski at meap dot de
Operating system: Windows XP
PHP version:      5.2.9
PHP Bug Type:     SPL related
Bug description:  RegexIterator / RecursiveIteratorIterator fails on nested 
arrays

Description:
------------
It is impossible to iterate through the children of a tree structure with
a RecursiveRegexIterator combined with a RecursiveIteratorIterator.

It only works when using the regex /A/, /r/ or /y/, because this
characters are in the word "Array".

See line 74 of ext/spl/internal/regexiterator.inc:
    
$subject = ($this->flags & self::USE_KEY) ? $this->key : $this->current;

When iterating over an array of arrays (e.g. array[3] in the above
example) "$subject" gets the value of "$this->current". In the case of an
arraym this value will be "Array".

This explains, why in the above example the regex "/y/" works, while "/v/"
is not. "/y/" finds a match in "Array".
    
The methode "accept()" of the RegexIterator-Object should first check,
whether an array has children, when working on nested arrays. If a nested
array is found, there is no sense in matching the regex against "Array".
    
Possible workaround could be adding the following lines above line 74 in
ext/spl/internal/regexiterator.inc:
    
if (is_array($subject) == true) {
   return true;
}

Reproduce code:
---------------
$array = array();
$array[0] = 'one';
$array[1] = 'two';
$array[2] = 'three';
$array[3] = array('four', 'five', 'xxx', 'yyy');
$array[4] = 'six';
$array[5] = 'seven';

// Correct output: 
// "yyy - "
$recArrayIt = new RecursiveArrayIterator($array);
$recRegexIt = new RecursiveRegexIterator($recArrayIt, '/y/');
$recItIt    = new RecursiveIteratorIterator($recRegexIt);

foreach($recItIt as $element) {
    echo $element.' - ';
}

// Wrong output: "seven - "
// Should be   : "five - seven -"

$recArrayIt = new RecursiveArrayIterator($array);
$recRegexIt = new RecursiveRegexIterator($recArrayIt, '/v/');
$recItIt    = new RecursiveIteratorIterator($recRegexIt);

foreach($recItIt as $element) {
    echo $element.' - ';
}


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

Reply via email to