php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #38463 Order of replacement using array in str_replace not documented
Submitted: 2006-08-15 08:29 UTC Modified: 2006-10-15 05:06 UTC
From: IFokkema at Ileos dot nl Assigned:
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: All
Private report: No CVE-ID: None
 [2006-08-15 08:29 UTC] IFokkema at Ileos dot nl
Description:
------------
Hi guys,

After a discussion in the list, I've reread the manual page (http://www.php.net/str_replace) and the comments, checked Google, but need some confirmation.

When using an array as the search argument in str_replace(), the array is processed first to last. As expected, but this behaviour is not documented and therefor not reliable for my project.

$s = "Input\r\nMore input\rSome more\nEtc";
echo str_replace(array("\r\n", "\n", "\r"), '<BR>', $s);

This works as expected (putting exactly one <BR> between every line of input). Related comment on the manual page is from "jr at adslate dot com (03-Jun-2004 02:52)".

But since it's not documented, I can't rely on this. If the order of replacement is ever switched, my code stops working properly.

Now I have to do this, to process all OS specific line breaks correctly:
$s = str_replace("\r\n", "\n", $s);
$s = str_replace("\r", "\n", $s);
echo str_replace("\n", '<BR>', $s);

Can you guys please document the order of handling the search array?

Thanks a bunch for all of your work.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-10-15 05:06 UTC] dave@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Aug 03 22:00:03 2025 UTC