php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33530 str_replace replaces nulls with empty strings
Submitted: 2005-06-30 23:55 UTC Modified: 2005-07-01 00:08 UTC
From: tony at marston-home dot demon dot co dot uk Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5.0.4 OS: Windows XP
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: tony at marston-home dot demon dot co dot uk
New email:
PHP Version: OS:

 

 [2005-06-30 23:55 UTC] tony at marston-home dot demon dot co dot uk
Description:
------------
When I use str_replace on an associative array to replace '<br>' with "\n" I find that values that were null have been replaced with empty strings. This affects subsequent processing as an empty string is not the same as null.

Reproduce code:
---------------
$pattern[]   = "<br>";
$pattern[]   = "<br/>";
$pattern[]   = "<br />";
$replacement = "\n";

$array = array('field1' => '<br>', 'field2' => '', 'field3' => null)
        
$array = str_replace($pattern, $replacement, $array);


Expected result:
----------------
Null values should be left as null values, and not replaced with empty strings.

Actual result:
--------------
Values in the array that were null are being replaced with empty strings.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-07-01 00:08 UTC] tony2001@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

str_replace() operates on strings and returns array of strings.
of course, NULL, false and others get converted to "" even before str_replace() begin to work with them.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 05:01:33 2025 UTC