php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71188 str_replace converts integers in original $search array to strings
Submitted: 2015-12-21 23:31 UTC Modified: -
From: admin at supermartas dot cz Assigned:
Status: Closed Package: Strings related
PHP Version: 7.0.1 OS: Any
Private report: No CVE-ID:
 [2015-12-21 23:31 UTC] admin at supermartas dot cz
Description:
------------
If I try the test script on Windows 10 or (probably) on CentOS, I will get this result:

array(3) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [2]=>
  int(2)
}
array(3) {
  [0]=>
  string(1) "0"
  [1]=>
  string(1) "1"
  [2]=>
  string(1) "2"
}

PHP converted all integers in $a array to strings but the original array should stay untouched!
But it's even worse on Ubuntu 14.04.3 LTS (PHP Version 7.0.1-2+deb.sury.org~trusty+1) where it returns at first the same result as on the other operating systems but after some page refreshes (press F5 in your browser about 20 times) it returns:

array(3) {
  [0]=>
  string(1) "0"
  [1]=>
  string(875835694) "<br />
<b>Fatal error</b>:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 3688507696953442304 bytes)

or sometimes:

array(3) {
  [0]=>
  string(1862275224) "<br />
<b>Fatal error</b>:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 140065040769024 bytes)

In another script (where I found out there is something wrong with PHP) it didn't throw that fatal error but it modified the $a array like this (original $a array has 10 items):

array(10) {
  [0]=>
  string(5) "https"
  [1]=>
  string(5) "https"
  [2]=>
  string(0) ""
  [3]=>
  string(5) "15421"
  [4]=>
  string(5) "15421"
  [5]=>
  string(7) "CGI/1.1"
  [6]=>
  string(6) "HTTP/2"
  [7]=>
  string(6) "HTTP/2"
  [8]=>
  string(5) "close"
  [9]=>
  string(1) "0"
}

If I convert items of $a array to string by hand ($a = ["0", "1", "2"]), everything is working correctly.

Test script:
---------------
<?php
	$a = [0, 1, 2];
	$b = ["Nula", "Jedna", "Dva"];

	var_dump($a);
	str_replace($a, $b, "1");
	var_dump($a);


Expected result:
----------------
array(3) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [2]=>
  int(2)
}
array(3) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [2]=>
  int(2)
}

Actual result:
--------------
array(3) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [2]=>
  int(2)
}
array(3) {
  [0]=>
  string(1) "0"
  [1]=>
  string(1) "1"
  [2]=>
  string(1) "2"
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-12-22 03:08 UTC] laruence@php.net
Automatic comment on behalf of laruence@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=3524849f77c1bd7fa582b256b891171c8ca4f830
Log: Fixed #71188 (str_replace converts integers in original $search array to strings)
 [2015-12-22 03:08 UTC] laruence@php.net
-Status: Open +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=3524849f77c1bd7fa582b256b891171c8ca4f830
Log: Fixed #71188 (str_replace converts integers in original $search array to strings)
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Tue Aug 29 15:01:52 2017 UTC