php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71178 preg_replace with arrays creates [0] in replace array if not already set
Submitted: 2015-12-21 07:15 UTC Modified: 2015-12-21 07:33 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: pglewis13 at gmail dot com Assigned:
Status: Closed Package: PCRE related
PHP Version: 7.0.0 OS: Linux
Private report: No CVE-ID: None
 [2015-12-21 07:15 UTC] pglewis13 at gmail dot com
Description:
------------
If pattern and replace are single element arrays and the keys are non-zero integers, then preg_replace may modify the replace parameter.  In the case of the test script, the replace parameter has an empty string inserted as the 0th entry after the call to preg_replace.  

I have not tested string keys or with more than one array element.  

Test script:
---------------
https://gist.github.com/pglewis/42c7a6e3da5e465793e0

Expected result:
----------------
I could find no caveats regarding keys in the documentation, so the expectation is that the replace parameter should not be modified in any way by a call to preg_replace.  

Actual result:
--------------
An empty string is inserted into the replace parameter as an extra array element, resulting in an unexpected result and breaking backward compatibility.  

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-12-21 07:33 UTC] requinix@php.net
-Summary: preg_replace: non-zero array keys for pattern/replace do not work and break BC +Summary: preg_replace with arrays creates [0] in replace array if not already set -Status: Open +Status: Verified -PHP Version: 7.0.1 +PHP Version: 7.0.0
 [2015-12-21 07:33 UTC] requinix@php.net
5.x unaffected. https://3v4l.org/Zhf6v
 [2015-12-21 10:11 UTC] laruence@php.net
Automatic comment on behalf of laruence@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=817513af4ec5e5a45fdd7ecd0a00e728d00c3ff3
Log: Fixed bug #71178 (preg_replace with arrays creates [0] in replace array if not already set)
 [2015-12-21 10:11 UTC] laruence@php.net
-Status: Verified +Status: Closed
 [2016-07-20 11:34 UTC] davey@php.net
Automatic comment on behalf of laruence@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=817513af4ec5e5a45fdd7ecd0a00e728d00c3ff3
Log: Fixed bug #71178 (preg_replace with arrays creates [0] in replace array if not already set)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC