php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #77994 Missing Detail in str_replace Documentation
Submitted: 2019-05-09 06:13 UTC Modified: 2019-05-09 06:33 UTC
From: mark at manngo dot net Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
21 + 46 = ?
Subscribe to this entry?

 
 [2019-05-09 06:13 UTC] mark at manngo dot net
Description:
------------
---
From manual page: https://php.net/function.str-replace
---

The document makes no mention of what happens if the replace array is smaller than the search array.

It appears that the search characters will be replaced with an empty string. This may or may not be expected, as the alternative result might have been to ignore the characters.

Test script:
---------------
$string='apple banana cherry apple banana cherry apple banana cherry';
$search=['apple','banana'];

$replace=['accordion','banjo'];
print str_replace($search,$replace,$string);

print "\n";

$replace=['accordion'];
print str_replace($search,$replace,$string);



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-05-09 06:18 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2019-05-09 06:18 UTC] requinix@php.net
Sure it does.

> If replace has fewer values than search, then an empty string is used for the rest of replacement values.
 [2019-05-09 06:25 UTC] mark at manngo dot net
Gad, you’re right. Sorry, I missed that.

It might help to include an example, though.
 [2019-05-09 06:33 UTC] requinix@php.net
I suppose? It's an unusual usage of the function. In fact I'd bet that most of the time someone uses str_replace with arrays (strtr is easier) and the two arrays aren't the same size, it's a bug - or at least unintentional.

If you have a use case in mind, you can hit the Edit link in the top-right corner and submit a patch including an example. (Or amend the existing "gotchas" example.) The docs are written in XML but there should be enough there to understand how it works.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Fri Sep 25 07:01:23 2020 UTC