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
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: 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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 01:01:30 2024 UTC