Task #54500

Use arrays in str_replace() calls

Added by Michiel Roos about 6 years ago. Updated over 1 year ago.

Status:
Closed
Priority:
Should have
Assignee:
-
Category:
Performance
Target version:
Start date:
2013-12-18
Due date:
% Done:

100%

TYPO3 Version:
6.2
PHP Version:
5.3
Tags:
Complexity:
easy
Sprint Focus:

Description

Multiple consecutive calls to str_replace can be replaced with a single call if the subject is the same.

This will speed up the replacement and in some cases has the benefit of being able to return or use the result of the str_replace directly without creating an intermediate variable.

Bad:
$lala = 'something';
$lala = str_replace('so', 'the', $lala);
$lala = str_replace('me', ' ', $lala);
$lala = str_replace('thing', 'other', $lala);

Good:
$lala = str_replace(
array('so', 'me', 'thing'),
array('the', ' ', 'other'),
'something'
);

Associated revisions

Revision ff2933b8 (diff)
Added by Michiel Roos about 6 years ago

[TASK] Use arrays in str_replace() calls

Multiple consecutive calls to str_replace can be replaced with a single
call if the subject is the same.

This will speed up the replacement and in some cases has the benefit of
being able to return or use the result of the str_replace directly without
creating an intermediate variable.

Bad:
$lala = 'something';
$lala = str_replace('so', 'the', $lala);
$lala = str_replace('me', ' ', $lala);
$lala = str_replace('thing', 'other', $lala);

Good:
$lala = str_replace(
array('so', 'me', 'thing'),
array('the', ' ', 'other'),
'something'
);

Change-Id: I1a2b99cce1444d3d2191eba8674f74aa2f74f0e8
Resolves: #54500
Releases: 6.2
Reviewed-on: https://review.typo3.org/26481
Reviewed-by: Markus Klein
Tested-by: Markus Klein

History

#1 Updated by Gerrit Code Review about 6 years ago

  • Status changed from New to Under Review

Patch set 1 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/26481

#2 Updated by Gerrit Code Review about 6 years ago

Patch set 2 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/26481

#3 Updated by Gerrit Code Review about 6 years ago

Patch set 3 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/26481

#4 Updated by Gerrit Code Review about 6 years ago

Patch set 4 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/26481

#5 Updated by Mathias Schreiber about 6 years ago

IIRC we are not to change adodb code since it if 3rd party.
It would be awesome if you could just revert those changes.
Apart from that... great catch.

#6 Updated by Gerrit Code Review about 6 years ago

Patch set 5 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/26481

#7 Updated by Gerrit Code Review about 6 years ago

Patch set 6 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/26481

#8 Updated by Gerrit Code Review about 6 years ago

Patch set 7 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/26481

#9 Updated by Michiel Roos about 6 years ago

  • Status changed from Under Review to Resolved
  • % Done changed from 0 to 100

#10 Updated by Benni Mack over 1 year ago

  • Status changed from Resolved to Closed

Also available in: Atom PDF