php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44925 preg_grep and array index - follow up on #44191
Submitted: 2008-05-06 09:49 UTC Modified: 2008-08-14 14:39 UTC
From: admin at ifyouwantblood dot de Assigned: nlopess (profile)
Status: Closed Package: PCRE related
PHP Version: 5.2.6 OS:
Private report: No CVE-ID: None
 [2008-05-06 09:49 UTC] admin at ifyouwantblood dot de
Description:
------------
this is a follow up on bug #44191. that was fixed, but everything inside the array is now converted to a string. as i understand it, the search array shouldn't change at all, so i think this is a bug. please note that with objects without a __toString() method, this of course leads to a fatal error.

another thing is, preg_grep issues a warning if you give it an object instead of converting the object to an array (like other function like array_flip() do)

addtionally a question: how should preg_grep react on multi-dimensional arrays anyways? convert them to a string and try to match the pattern? go through every level and return a multi-dimensional array? issue a warning?

Reproduce code:
---------------
$array=Array("1",2,3,1.1,FALSE,NULL,Array());

var_dump($array); echo "<br>\n";

var_dump(preg_grep('/asdf/',$array)); echo "<br>\n";

var_dump($array); echo "<br>\n";

var_dump(preg_grep('/asdf/',new stdClass)); echo "<br>\n";

Expected result:
----------------
array(8) { [0]=> string(1) "1" [1]=> int(2) [2]=> int(3) [3]=> float(1.1) [4]=> bool(false) [5]=> NULL [6]=> array(0) { } [7]=> object(stdClass)#8 (0) { } } 

array(0) { }

array(8) { [0]=> string(1) "1" [1]=> int(2) [2]=> int(3) [3]=> float(1.1) [4]=> bool(false) [5]=> NULL [6]=> array(0) { } }  

array(0) { }

Actual result:
--------------
array(7) { [0]=> string(1) "1" [1]=> int(2) [2]=> int(3) [3]=> float(1.1) [4]=> bool(false) [5]=> NULL [6]=> array(0) { } } 

Notice: Array to string conversion in D:\_projects\preg_grep.php

array(0) { } 

array(7) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(3) "1.1" [4]=> string(0) "" [5]=> string(0) "" [6]=> string(5) "Array" }

Warning: preg_grep() expects parameter 2 to be array, object given in D:\_projects\cms\admin\data\classes\class.article.php on line 13
NULL 



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-05-06 10:57 UTC] felipe@php.net
> this is a follow up on bug #44191. that was fixed, but everything > inside
> the array is now converted to a string. as i understand it, the search
> array shouldn't change at all, so i think this is a bug. please note
> that with objects without a __toString() method, this of course leads to
> a fatal error.

This is expected, the function is for matching strings.

> another thing is, preg_grep issues a warning if you give it an object
> instead of converting the object to an array (like other function like
> array_flip() do)

Exactly, preg_grep() is intended for works only with arrays.

> addtionally a question: how should preg_grep react on multi-
> dimensional
> arrays anyways? convert them to a string and try to match the pattern?
> go through every level and return a multi-dimensional array? issue a
> warning?

The PHP converts for the literal string 'Array'.
That can be viewed with: var_dump(preg_grep('//', array(array())));



Thanks.
 [2008-05-06 12:48 UTC] admin at ifyouwantblood dot de
>> this is a follow up on bug #44191. that was fixed, but everything
>> inside the array is now converted to a string. as i understand it,
>> the search array shouldn't change at all, so i think this is a
>> bug. please note that with objects without a __toString() method, >> this of course leads to a fatal error.
>
> This is expected, the function is for matching strings.

sorry, but did you even take a look at the samples? preg_grep is a SEARCH function, why should it change the INPUT array?
 [2008-05-06 13:07 UTC] felipe@php.net
Well, preg_grep() != in_array()...
PHP obviously should convert the values to string to be used in regex matching. Hence, i think that it should returns the string that was matched.

Anyway, i'll assign to the maintainer to solve this issue.

Thanks.
 [2008-05-06 16:00 UTC] admin at ifyouwantblood dot de
> PHP obviously should convert the values to string to be used in
> regex matching. Hence, i think that it should returns the string
> that was matched.

sure, internally it'll has to be converted, but i see no reason for a change of the input array. thus preg_grep should work with a copy of the input array...
 [2008-07-06 18:46 UTC] nlopess@php.net
I don't really know what to do with this..
While I agree it shouldn't modify the original array, changing the behavior would break BC.. Any opinions about this?
 [2008-07-13 16:18 UTC] jani@php.net
Functions should never ever modify the input array unless designed to do so. According to documentation, preg_grep() is not supposed to modify the input array. So it's a bug, not BC. What exactly is the usage case where it should modify input array?
 [2008-07-17 15:46 UTC] nlopess@php.net
non, really. It's just that the code will become much bigger to do the copying :)
 [2008-08-14 14:39 UTC] nlopess@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC