php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48130 RegexIterator / RecursiveIteratorIterator fails on nested arrays
Submitted: 2009-05-02 12:30 UTC Modified: 2009-05-14 01:00 UTC
From: richard dot piatkowski at meap dot de Assigned:
Status: No Feedback Package: SPL related
PHP Version: 5.2.9 OS: Windows XP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: richard dot piatkowski at meap dot de
New email:
PHP Version: OS:

 

 [2009-05-02 12:30 UTC] richard dot piatkowski at meap dot de
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.' - ';
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-05-06 15:47 UTC] jani@php.net
What exactly is the expected and what is actual output? 
Also, reproduce scripts should be as short as possible, start with <?php 
and end with ?>
 [2009-05-14 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC