Skip to content

[5.8] Fix recursive replacements in Str::replaceArray()#28338

Merged
taylorotwell merged 1 commit into
laravel:5.8from
staudenmeir:replace-array
Apr 25, 2019
Merged

[5.8] Fix recursive replacements in Str::replaceArray()#28338
taylorotwell merged 1 commit into
laravel:5.8from
staudenmeir:replace-array

Conversation

@staudenmeir
Copy link
Copy Markdown
Contributor

Str::replaceArray() recursively replaces the search value if it appears in the replacements:

Str::replaceArray('?', ['foo?', 'bar'], '?, ?');

// expected: foo?, bar
// actual:   foobar, ?

Laravel uses the method in query exceptions to replace the ? placeholders with the actual bindings. This causes incorrect results if a binding contains a ?.

We can fix it splitting the string and gluing it back together.

Theoretically, this could be a breaking change for users who are relying on recursive replacements. But I don't a see case where you would want this behavior.

Fixes #28245.

@taylorotwell taylorotwell merged commit ed16959 into laravel:5.8 Apr 25, 2019
@staudenmeir staudenmeir deleted the replace-array branch April 25, 2019 22:07
@vlakoff
Copy link
Copy Markdown
Contributor

vlakoff commented May 16, 2019

I should review this code, but are you sure it works when the first replacement doesn't occur at the very beginning?

@vlakoff
Copy link
Copy Markdown
Contributor

vlakoff commented May 16, 2019

… or when the parts between the replacements are different.

If there is actually such breakage, the tests would have to be improved as well ;)

@vlakoff
Copy link
Copy Markdown
Contributor

vlakoff commented May 16, 2019

Just in case, refs #12259, though the previous code had the same problem.

@staudenmeir
Copy link
Copy Markdown
Contributor Author

@vlakoff Yes, these cases work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bindings not correctly assigned in SQL exceptions

3 participants